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

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

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

 

posted @ 2023-07-07 09:28  Panax  阅读(10)  评论(0编辑  收藏  举报