javascript 将包含对象的数组展平为 1 个对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31136422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flatten array with objects into 1 object
提问by Szymon Toda
Given input:
给定输入:
[{ a: 1 }, { b: 2 }, { c: 3 }]
How to return:
如何返回:
{ a: 1, b: 2, c: 3 }
For arrays it's not a problemwith lodash but here we have array of objects.
对于数组,lodash不是问题,但这里我们有对象数组。
回答by Benjamin Gruenbaum
Use Object.assign
:
let merged = Object.assign(...arr); // ES6 (2015) syntax
var merged = Object.assign.apply(Object, arr); // ES5 syntax
Note that Object.assign
is not yet implemented in many environment and you might need to polyfill it (either with core-js, another polyfill or using the polyfill on MDN).
请注意,Object.assign
它尚未在许多环境中实现,您可能需要对其进行 polyfill(使用 core-js、另一个 polyfill 或使用 MDN 上的 polyfill)。
You mentioned lodash, so it's worth pointing out it comes with a _.assign
function for this purpose that does the same thing:
您提到了 lodash,因此值得指出的是,它带有一个_.assign
用于此目的的函数,该函数执行相同的操作:
var merged = _.assign.apply(_, [{ a: 1 }, { b: 2 }, { c: 3 }]);
But I really recommend the new standard library way.
但我真的推荐新的标准库方式。
回答by Adam Boduch
With lodash, you can use merge():
使用 lodash,您可以使用merge():
var arr = [ { a: 1 }, { b: 2 }, { c: 3 } ];
_.merge.apply(null, [{}].concat(arr));
// → { a: 1, b: 2, c: 3 }
If you're doing this in several places, you can make merge()
a little more elegant by using partial()and spread():
如果你在几个地方这样做,你可以merge()
通过使用partial()和spread()来更优雅一点:
var merge = _.spread(_.partial(_.merge, {}));
merge(arr);
// → { a: 1, b: 2, c: 3 }
回答by Michael Coxon
Here is a version not using ES6 methods...
这是一个不使用 ES6 方法的版本......
var arr = [{ a: 1 }, { b: 2 }, { c: 3 }];
var obj = {};
for(var i = 0; i < arr.length; i++) {
var o = arr[i];
for(var key in o) {
if(typeof o[key] != 'function'){
obj[key] = o[key];
}
}
}
console.log(obj);
fiddle: http://jsfiddle.net/yaw3wbb8/
回答by Vladimir
You can use underscore.extend function like that:
您可以像这样使用 underscore.extend 函数:
var _ = require('underscore');
var a = [{ a: 1 }, { b: 2 }, { c: 3 }];
var result = _.extend.apply(null, a);
console.log(result); // { a: 1, b: 2, c: 3 }
console.log(a); // [ { a: 1, b: 2, c: 3 }, { b: 2 }, { c: 3 } ]
And to prevent modifying original array you should use
并防止修改原始数组,您应该使用
var _ = require('underscore');
var a = [{ a: 1 }, { b: 2 }, { c: 3 }];
var result = _.extend.apply(null, [{}].concat(a));
console.log(result); // { a: 1, b: 2, c: 3 }
console.log(a); // [ { a: 1 }, { b: 2 }, { c: 3 } ]
回答by jonny
I've got a neat little solution not requiring a polyfill.
我有一个不需要 polyfill 的简洁小解决方案。
var arr = [{ a: 1 }, { b: 2 }, { c: 3 }];
var object = {};
arr.map(function(obj){
var prop = Object.getOwnPropertyNames(obj);
object[prop] = obj[prop];
});
Hope that helps :)
希望有帮助:)
回答by deed02392
Here is a nice usage of Object.assign
with the array.prototype.reduce
function:
这是Object.assign
与array.prototype.reduce
函数的一个很好的用法:
let merged = arrOfObjs.reduce((accum, val) => {
Object.assign(accum, val);
return accum;
}, {})
This approach does not mutate the input array of objects, which could help you avoid difficult to troubleshoot problems.
这种方法不会改变对象的输入数组,这可以帮助您避免难以解决的问题。
回答by deed02392
You can easily flat your object to array.
您可以轻松地将对象平铺到阵列。
function flatten(elements) {
return elements.reduce((result, current) => {
return result.concat(Array.isArray(current) ? flatten(current) : current);
}, []);
};