javascript - is there an alternative on lodash to _.merge.apply(_, x); where x is an array of objects? - Stack Overflow

I want to merge all the properties from an array of objects into one single object using lodash? I can

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?

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?

Share Improve this question asked May 9, 2016 at 20:18 GabroeGabroe 371 silver badge4 bronze badges 3
  • I don't think there it, but why don't you want to use apply? – Andrew Burgess Commented May 9, 2016 at 20:23
  • 1 there are a couple here and here, is there a reason you want to avoid apply or lodash? EDIT: ah, i see, you strictly want to use lodash, nevermind this ment then – aug2uag Commented May 9, 2016 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 Commented May 9, 2016 at 21:21
Add a ment  | 

5 Answers 5

Reset to default 2

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.

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);
}, {});

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)

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./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.

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>");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745575696a4633948.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信