How to group an array of objects by key

后端 未结 24 2913
后悔当初
后悔当初 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:53

    You can try to modify the object inside the function called per iteration by _.groupBy func. Notice that the source array change his elements!

    var res = _.groupBy(cars,(car)=>{
        const makeValue=car.make;
        delete car.make;
        return makeValue;
    })
    console.log(res);
    console.log(cars);
    

提交回复
热议问题