如何连接多个 JavaScript 对象的属性
我正在寻找“添加”多个 JavaScript 对象(关联数组)的最佳方法。
例如,给定:
a = { "one" : 1, "two" : 2 };
b = { "three" : 3 };
c = { "four" : 4, "five" : 5 };
最好的计算方法是什么:
{ "one" : 1, "two" : 2, "three" : 3, "four" : 4, "five" : 5 }
I am looking for the best way to "add" multiple JavaScript objects (associative arrays).
For example, given:
a = { "one" : 1, "two" : 2 };
b = { "three" : 3 };
c = { "four" : 4, "five" : 5 };
what is the best way to compute:
{ "one" : 1, "two" : 2, "three" : 3, "four" : 4, "five" : 5 }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
ECMAscript 6 引入了
Object.assign()
来在 Javascript 中实现这一点。有关 Object.assign() 的 MDN 文档
许多现代浏览器都支持
Object.assign
但还不是全部。使用 Babel 和 Traceur 用于生成向后兼容的 ES5 JavaScript。ECMAscript 6 introduced
Object.assign()
to achieve this natively in Javascript.MDN documentation on Object.assign()
Object.assign
is supported in many modern browsers but not yet all of them. Use a transpiler like Babel and Traceur to generate backwards-compatible ES5 JavaScript.ECMAScript 6 具有扩展语法。现在你可以这样做:
ECMAScript 6 has spread syntax. And now you can do this:
这应该可以做到:
输出:
This should do it:
Output:
您可以像这样使用 jquery 的
$.extend
:You could use jquery's
$.extend
like this:Underscore 有几种方法可以做到这一点;
1. _.extend(destination, *sources)
复制源中的所有属性 对象传递给 destination 对象,并返回 destination 对象。
或
2。 _.defaults(object, *defaults)
填写未定义属性在对象中使用来自默认对象的值,并返回对象。
或者
Underscore has few methods to do this;
1. _.extend(destination, *sources)
Copy all of the properties in the source objects over to the destination object, and return the destination object.
Or
2. _.defaults(object, *defaults)
Fill in undefined properties in object with values from the defaults objects, and return the object.
Or
现在可以使用比
扩展语法对于 对象文字 已在 ECMAScript 2018):
传播 (...) 许多现代浏览器都支持运算符< /a> 但不是全部。
因此,建议使用像 Babel 将 ECMAScript 2015+ 代码转换为当前和旧版浏览器或环境中向后兼容的 JavaScript 版本。
这是 Babel 将为您生成的等效代码:
Shallow-cloning (excluding prototype) or merging of objects is now possible using a shorter syntax than Object.assign().
Spread syntax for object literals was introduced in ECMAScript 2018):
Spread (...) operator is supported in many modern browsers but not all of them.
So, it is recommend to use a transpiler like Babel to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments.
This is the equivalent code Babel will generate for you:
要合并动态数量的对象,我们可以使用
Object.assign
与 扩展语法。上面的函数接受任意数量的对象,将它们的所有属性合并到一个新对象中,其中后面对象的属性覆盖以前对象的属性。
演示:
要合并对象数组,可以应用类似的方法。
演示:
To merge a dynamic number of objects, we can use
Object.assign
with spread syntax.The above function accepts any number of objects, merging all of their properties into a new object with properties from later objects overwriting those from previous objects.
Demo:
To merge an array of objects, a similar method may be applied.
Demo:
为什么函数应该限制为 3 个参数?另外,检查
hasOwnProperty
。Why should the function be restricted to 3 arguments? Also, check for
hasOwnProperty
.使用ES7展开运算符来表示对象很容易,在浏览器控制台中输入
It's easy using ES7 spread operator for an object, in your browser console put
注意:先前对象中的现有属性将被覆盖。
Notice: Existing properties in previous objects will be overwritten.
ES6 ++
问题是将各种不同对象添加到一个对象中。
假设您有一个具有多个对象键的对象:
这是我找到的解决方案(仍然必须 foreach :/)
通过这样做,我们可以动态地将所有对象键添加到一个对象中。
ES6 ++
The question is adding various different objects into one.
lets say you have one object with multiple keys that are objects:
this was the solution I found (still have to foreach :/)
By doing this we can dynamically add ALL object keys into one.
也许,最快、有效和更通用的方法是这样的(您可以合并任意数量的对象,甚至复制到第一个对象 -> 分配):
它还允许您在第一个对象通过引用传递时修改它。
如果您不想要这个,但想要一个包含所有属性的全新对象,那么您可以传递 {} 作为第一个参数。
组合对象和对象1都包含对象1、对象2、对象3的属性。
在本例中,combined_object 包含 object1、object2、object3 的属性,但 object1 未修改。
检查此处: https://jsfiddle.net/ppwovxey/1/
注意:JavaScript 对象通过参考。
Probably, the fastest, efficient and more generic way is this (you can merge any number of objects and even copy to the first one ->assign):
It also allows you to modify the first object as it passed by reference.
If you don't want this but want to have a completely new object containing all properties, then you can pass {} as the first argument.
combined_object and object1 both contain the properties of object1,object2,object3.
In this case, the combined_object contains the properties of object1,object2,object3 but object1 is not modified.
Check here: https://jsfiddle.net/ppwovxey/1/
Note: JavaScript objects are passed by reference.
最简单:扩展运算符
Simplest: spread operators