admin管理员组文章数量:1432640
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
5 Answers
Reset to default 2With 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>");
本文标签:
版权声明:本文标题:javascript - is there an alternative on lodash to _.merge.apply(_, x); where x is an array of objects? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745575695a2664314.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论