admin管理员组文章数量:1394051
Consider this:
let obj1 = {id: 1, mon_key: "one", property1: "property_one"}
let obj2 = {id: 1, mon_key: "two", property2: "property_two'"}
let merged = {...obj1, ...obj2}
console.log(merged)
Output:
{id: 1, mon_key: "two", property1: "property_one", property2: "property_two'"}
We see that mon_key
from obj2
is taken over (value - "two"
) in this merge.
Does this mean where when there is a collision of mon key in an object spread (merge) it is guaranteed that the one that e later wins? If yes, is this mentioned somewhere in ES6 spec?
Consider this:
let obj1 = {id: 1, mon_key: "one", property1: "property_one"}
let obj2 = {id: 1, mon_key: "two", property2: "property_two'"}
let merged = {...obj1, ...obj2}
console.log(merged)
Output:
{id: 1, mon_key: "two", property1: "property_one", property2: "property_two'"}
We see that mon_key
from obj2
is taken over (value - "two"
) in this merge.
Does this mean where when there is a collision of mon key in an object spread (merge) it is guaranteed that the one that e later wins? If yes, is this mentioned somewhere in ES6 spec?
- 4 It's just the sequence of order, and the fact that ids are not repeated. The logic adds all the key values from the first object to the new object, and then it does the same with the second object. Since keys are not duplicated, they replace anything that pre-exists. – Taplar Commented Aug 21, 2020 at 17:23
-
6
Same way as if you wrote
var x = {'a': 1, 'a': 2 }
. The value ofx.a
at the end would be 2 – Taplar Commented Aug 21, 2020 at 17:24 - 2 Given that this is the behavior of objects, I wouldn't be surprised if it was not mentioned in the specs for basic objects. – Taplar Commented Aug 21, 2020 at 17:26
- 2 Last override wins. Its the basic strategy of most programming languages. – SILENT Commented Aug 21, 2020 at 17:26
-
1
Spec tc39.es/ecma262/2020/…. You need to unwind
PropertyDefinition:...AssignmentExpression
part. – Yury Tarabanko Commented Aug 21, 2020 at 17:41
4 Answers
Reset to default 3You could check this section in MDN doc, Spread in object literals
The Rest/Spread Properties for ECMAScript proposal (ES2018) added spread properties to object literals. It copies own enumerable properties from a provided object onto a new object
Shallow-cloning (excluding prototype) or merging of objects is now possible using a shorter syntax than
Object.assign()
const obj1 = { foo: 'bar', x: 42 }; const obj2 = { foo: 'baz', y: 13 }; const mergedObj = { ...obj1, ...obj2 }; // Object { foo: "baz", x: 42, y: 13 }
The rest object spread is just syntactic sugar for Object.assign
(with the exclusion of setters)
The spec here states how Object.assign(target, ...sources)
operates:
- For each element
nextSource
ofsources
, in ascending index order, do
Where it basically does a forEach
loop for each of the sources in ascending order => Set
s each OwnProperty key
/value
to the target
object(to
).
MDN also mentions this in § Merging objects with same properties:
The properties are overwritten by other objects that have the same properties later in the parameters order.
Checkout https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#Spread_in_object_literals
If you have mon properties the priority will be right to left. The object the most at the right will have priority over the one at its left.
Yes. You will always get the property of the subsequent object.
It doesn't directly mention that on MDN, but the spread operator applies a shallow parison and assigns the subsequent property to the object.
https://dmitripavlutin./object-rest-spread-properties-javascript/#21-object-spread-rule-latter-property-wins
You can use the spread operator to create immutable objects this way
let obj = { one: 1, two: 2, three: 3 }
let _obj = {...obj, three: 4};
console.log(_obj);
Output
{
"one": 1,
"two": 2,
"three": 4
}
And using multiple objects...
let obj = { one: 1, two: 2, three: 3 }
let _obj = {...obj, three: 4};
_obj.four = 4;
let __obj = {...obj, ..._obj}
console.log(__obj);
Output
{
"one": 1,
"two": 2,
"three": 4,
"four": 4
}
本文标签: javascriptObject spread with common keysStack Overflow
版权声明:本文标题:javascript - Object spread with common keys - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744089173a2589099.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论