How can you merge objects in array of objects?

前端 未结 12 1302
小蘑菇
小蘑菇 2020-12-06 17:32

I\'m looking for the best solution to merge all objects in one array

const arrayOfObjects = [
 {name: \'Fred\', surname: \'Shultz\'}, {name: \'Anne\', surnam         


        
12条回答
  •  Happy的楠姐
    2020-12-06 18:03

    Here is a lodash approach

      _(input).flatMap(_.entries).groupBy(0).mapValues(v => _.map(v, 1)).value()
    

    var input = [
     {name: 'Fred', surname: 'Shultz'}, {name: 'Anne', surname: 'Example'}
    ];
    
    var res =   _(input).flatMap(_.entries).groupBy(0).mapValues(v => _.map(v, 1)).value()
    
    console.log(res);

    This will take care if the objects doesn't have exactly same key sets

提交回复
热议问题