1

I want to merge all the properties from an array of objects into one single object using lodash? I can iterate the array or call apply on _.merge:

const arr = [{1:1, 2:2},{3:3},{4:4}];

_.merge.apply(_, arr); //{1:1, 2:2, 3:3, 4:4};

is there an alternative without using apply?

Gabroe
  • 37
  • 1
  • 4
  • I don't think there it, but why don't you want to use `apply`? – Andrew Burgess May 09 '16 at 20:23
  • 1
    there are a couple [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) and [here](http://stackoverflow.com/questions/19874555/how-to-convert-array-of-objects-into-one-object-in-javascript), is there a reason you want to avoid apply or lodash? EDIT: ah, i see, you strictly want to use lodash, nevermind this comment then – aug2uag May 09 '16 at 20:23
  • It was more out of curiosity with lodash API but seems like there is not a function that is doing that specifically. Thanks! – Gabroe May 09 '16 at 21:21

5 Answers5

1

Working Example

You could use each and extend:

var o = {};
var arr = [{1:1, 2:2},{3:3},{4:4}];
_.each(arr, function(e) {
  _.extend(o, e);
});

or reduce:

var otherWay = _.reduce(arr, function(obj, next) {
  return _.extend(obj, next);
}, {});
omarjmh
  • 13,632
  • 6
  • 34
  • 42
1

looks like the prefect situation to use [].reduce (JS not lodash) :

const arr = [{1:1, 2:2},{3:3},{4:4}];


const r = arr.reduce((ac,x) => Object.assign(ac,x),{})

console.log(r)
maioman
  • 18,154
  • 4
  • 36
  • 42
1

With ES6, you can do this:

const arr = [{1:1, 2:2},{3:3},{4:4}];

_.merge(...arr);

You should only do this if you're only targeting new browsers that support this, or if you're using a transpiler like Babel.

If you don't want to use ES6, then there's no reason to not use .apply. It's part of Javascript. Lodash has no need to re-invent this feature. You shouldn't expect a library to do everything and replace the language itself. The other loop-based answers (.reduce, .each, etc.) are needlessly less efficient than they need to be, since both _.merge and Object.assign support more than two parameters.

Macil
  • 3,575
  • 1
  • 20
  • 18
0

I will give a very silly answer. Well thinking it over, since Object.assign() just makes shallow copies this might practically work out better if you are not fond of recursive operations.

var arr = [{1:1, 2:2},{3:3},{4:4}],
 merged = JSON.parse(arr.reduce((s,o)=> s+JSON.stringify(o),"").replace(/}{/g,","));
document.write("<pre>" +JSON.stringify(merged,null,2) + "</pre>");
halfer
  • 19,824
  • 17
  • 99
  • 186
Redu
  • 25,060
  • 6
  • 56
  • 76
0

You can use reduce() with merge() as the iterator and an empty object as the base object for the merge to prevent the mutation any mutation of the items of the array.

var arr = [{1:1, 2:2},{3:3},{4:4}];

var result = _.reduce(arr, _.merge, {});

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.11.2/lodash.js"></script>

Note: The solution that you're using right now mutates the items of the array.

ryeballar
  • 29,658
  • 10
  • 65
  • 74