Concatenating array objects with lodash

I am trying to merge all objects together using lodash and I have tried _.assign and _.merge to still show objects separately.

 var arr = [
    {"asf" : 33, "ff1" : 12},{"xx" : 90, "ff2" : 13},{"xw" : 66, "ff3" : 176}
]
  console.log( _.assign({}, arr)); //should show {"asf" : 33, "ff1" : 12,"xx" : 90, "ff2" : 13, "xw" : 66, "ff3" : 176}

      

http://jsfiddle.net/ymppagdq/

+3


source to share


3 answers


Here's how you can do it:

_.assign.apply(_, arr);

      



Demo: http://jsfiddle.net/ymppagdq/2/

or _.reduce(arr, _.extend)

will work as well.

+11


source


The ES2015 you can use _.assign(...arr)

, or if you are really targeting only ES2015, Object.assign(...arr)

.



+2


source


If there is no method that accepts an array of objects, apply can be used to call with multiple arguments:

var arr = [...];
_.assign.apply(_, [{}].concat(arr))

      

0


source







All Articles