array.groupBy in TypeScript

后端 未结 4 1888
盖世英雄少女心
盖世英雄少女心 2021-02-19 18:50

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

4条回答
  •  失恋的感觉
    2021-02-19 19:05

    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);
    

提交回复
热议问题