简体   繁体   English

如何使用扩展语法从 JavaScript 中的多个未知对象复制 object 属性?

[英]How can I use spread syntax to copy object properties from multiple unknown objects in JavaScript?

I would like to use spread syntax to copy properties from several unknown objects into one object.我想使用扩展语法将几个未知对象的属性复制到一个 object 中。 Something like this:像这样的东西:

var array = [{a:0}, {b:1}, {c:2}]; // This array could hold any number of objects

// This function should take the objects in the array and use spread syntax to create a new object.
function merge(arr) { /* Code I wish I knew */ } // returns an object

var combo = merge(array);

console.log(combo); // {a:0, b:1, c:2}

I am aware of Object.assign .我知道Object.assign My reason for asking this question is just to see if this sort of thing can be done with spread syntax.我问这个问题的原因只是想看看这种事情是否可以用扩展语法来完成。 Additionally, I'm concerned with V8's Hidden Classes.此外,我关心 V8 的隐藏类。 From what I understand, modifying an object after it is instantiated negatively impacts the performance of the Hidden Class.据我了解,在实例化 object 后对其进行修改会对隐藏 Class 的性能产生负面影响。 I've read that Spread doesn't have this issue, possibly because it instantiates the object with all of the properties of the other objects.我读过Spread没有这个问题,可能是因为它用其他对象的所有属性实例化了object。

You can use a combination of Array.reduce with Object.assign :您可以将Array.reduceObject.assign结合使用:

 var array = [{a:0}, {b:1}, {c:2}]; function merge(arr) { return array.reduce((x,y) => Object.assign(x, y)) // // per the comments, if we don't want to mutate the original object we can do instead: // return array.reduce((x,y) => Object.assign(x, y), {}) } var combo = merge(array); console.log(combo);

You can use Array#reduce to iterate the array and merge the children into one object:您可以使用Array#reduce迭代数组并将子元素合并为一个 object:

 var array = [{a:0}, {b:1}, {c:2}]; function merge(arr) { return arr.reduce((acc, cur) => ({...acc, ...cur })); } var combo = merge(array); console.log(combo);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用对象解构扩展运算符来删除多个对象属性 - How to use object destructuring spread operator to delete multiple object properties ES6在对象数组上传播语法,并获取对象属性 - ES6 spread syntax on array of objects, and getting object properties 如何在 JavaScript 中将对象传播到类属性中 - How to spread an object into a classes properties in JavaScript 传播具有动态属性的嵌套javascript对象的语法以进行分组 - Spread syntax for nested javascript object with dynamic properties for grouping JavaScript - 在三元语句中使用扩展语法向 object 添加属性 - JavaScript - adding properties to an object using the spread syntax within a ternary statement 如何使用带有嵌套属性的 object 传播? - How to use object spread with nested properties? 如何使用扩展运算符使用扩展运算符使用反应从对象数组中删除 object? - How to use spread operator to delete an object from array of objects using spread operator using react? 如何在嵌套的 javascript 对象上使用扩展运算符? - How to use spread operator on nested javascript objects? 如何从JavaScript中的对象数组中选取属性? - How can I pick properties from an array of objects in JavaScript? 如何从 JavaScript object 中动态删除整个扩展运算符 - How I can remove the spread operator as whole dynamically from JavaScript object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM