Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging objects of an array using lodash

I'm trying to merge all objects together using lodash and I tried _.assign and _.merge still it shows the 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/

like image 942
user1184100 Avatar asked Oct 11 '14 20:10

user1184100


1 Answers

This is how you can do it:

_.assign.apply(_, arr);

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

or _.reduce(arr, _.extend) would also work.

like image 80
dfsq Avatar answered Oct 11 '22 14:10

dfsq