[英]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. 我正在尝试使用lodash将所有对象合并在一起,我尝试了_.assign和_.merge,它仍然分别显示对象。
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}
This is how you can do it: 这是你如何做到的:
_.assign.apply(_, arr);
or _.reduce(arr, _.extend)
would also work. 或
_.reduce(arr, _.extend)
. _.reduce(arr, _.extend)
也可以。
在ES2015中,您可以使用_.assign(...arr)
,或者如果您真的只针对ES2015, Object.assign(...arr)
。
If there isn't a method which accepts an array of objects, apply can be used to call with multiple arguments: 如果没有接受对象数组的方法,则可以使用apply来调用多个参数:
var arr = [...];
_.assign.apply(_, [{}].concat(arr))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.