I tried to "merge" two objects with Object.assign(), but somehow the original objects are overriden. See the example:
var x = {x:1};
var y = {y:2};
var z = Object.assign(x, y);
// what I've expected
console.log(z); // {"x": 1, "y": 2}
// what I haven't expected
console.log(x); // {"x": 1, "y": 2}
An empty object must be provided as the first argument of Object.assign for a new merged object to be created.
var x = {
x: 1
};
var y = {
y: 2
};
var z = Object.assign({}, x, y);
// new object
console.log(z); // {"x": 1, "y": 2}
// still the same
console.log(x); // {"x": 1}
console.log(y); // {"y": 2}
You can read more information about Object.assign here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With