const a = {x: "Hi", y: "Test"}
const b = ???
// b = {x: "Bye", y: "Test"}
// a = {x: "Hi", y: "Test"}
Two ways...
You can use Object.assign:
const a = { x: "Hi", y: "Test" }
const b = Object.assign({}, a, { x: "Bye" });
console.log(b);
Or you can use the object spread operator.
The spread operator allows you to "assign" all properties of one object to another.
We can use it here within an object literal to assign all properties of a
to b
:
const additions = { x: "Bye" }
const a = { x: "Hi", y: "Test" }
const b = { ...a, ...additions }
console.log(b);