js 数组按元素key分类,转换成对象结构

// arr 源数组 id 需要分类的目标key
let shop = arr.reduce((shop, item) => {
    shop[item.id] = shop[item.id] ? [...shop[item.id], item] : [item];
    return shop;
}, {});
console.log(shop);

 结果:

 

posted @ 2020-12-25 10:02  riven.lcs  阅读(519)  评论(0编辑  收藏  举报