把若干数组按指定的字段名进行分组

发布时间 2023-07-07 09:31:51作者: Panax
// 把若干数组按指定的字段名进行分组

function groupBy(list, propName) {
  return list.reduce((acc, item) => {
    const key = item[propName];
    if (!acc[key]) {
      acc[key] = [];
    }
    acc[key].push(item);
    return acc;
  }, {});
}