array.groupBy in TypeScript

后端 未结 4 1526
囚心锁ツ
囚心锁ツ 2021-02-19 19:01

The basic array class has .map, .forEach, .filter, and .reduce, but .groupBy i noticably absent, preventing me f

4条回答
  •  你的背包
    2021-02-19 19:10

    Instead of groupby use reduce. Suppose product is your array

    let group = product.reduce((r, a) => {
    console.log("a", a);
    console.log('r', r);
    r[a.organization] = [...r[a.organization] || [], a];
    return r;
    }, {});
    
    console.log("group", group);
    

提交回复
热议问题