// 分组
function groupBy(groups) {
  return groups.reduce((pre, cur) => {
    pre[cur.groupId] = (pre[cur.groupId] || []).concat(cur); // eslint-disable-line
    return pre;
  }, {});
}

const groupsStudents = [
  {
    groupId: 1,
    student: {
      name: 'shangyy',
      age: 18,
    },
  },
  {
    groupId: 2,
    student: {
      name: 'shangyy',
      age: 18,
    },
  },
  {
    groupId: 3,
    student: {
      name: 'shangyy',
      age: 18,
    },
  },
  {
    groupId: 1,
    student: {
      name: 'shangyy',
      age: 18,
    },
  },
  {
    groupId: 2,
    student: {
      name: 'shangyy',
      age: 18,
    },
  },
];
console.log(groupBy(groupsStudents));

// 联合
function groupAdd(group1, group2) {
  return group2.map(item => {
    const gItem = group1.find(g => g.groupId === item.groupId);
    return {
      ...item,
      ...gItem,
    };
  });
}

const group1 = [
  {
    groupId: 1,
    groupName: 'class1',
  },
  {
    groupId: 2,
    groupName: 'class2',
  },
];

console.log(groupAdd(group1, groupsStudents));

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-16
  • 2021-07-21
  • 2021-06-22
  • 2022-12-23
  • 2021-12-08
  • 2021-05-02
猜你喜欢
  • 2021-07-19
  • 2022-01-08
  • 2022-12-23
  • 2021-05-09
  • 2021-08-18
  • 2022-12-23
  • 2021-10-05
相关资源
相似解决方案