7

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/

3 Answers 3

16

This is how you can do it:

_.assign.apply(_, arr);

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

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

Sign up to request clarification or add additional context in comments.

Comments

9

In ES2015, you can use _.assign(...arr), or if you're really only targeting ES2015, Object.assign(...arr).

Comments

0

If there isn't a method which accepts an array of objects, apply can be used to call with multiple arguments:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.