How to group an array of objects by key

后端 未结 24 2912
后悔当初
后悔当初 2020-11-21 05:13

Does anyone know of a (lodash if possible too) way to group an array of objects by an object key then create a new array of objects based on the grouping? For example, I hav

24条回答
  •  爱一瞬间的悲伤
    2020-11-21 05:57

    Its also possible with a simple for loop:

     const result = {};
    
     for(const {make, model, year} of cars) {
       if(!result[make]) result[make] = [];
       result[make].push({ model, year });
     }
    

提交回复
热议问题