Is there a native js way to extend an object such that the original object is modified?
eg
const obj = {a: 1}
const obj2 = {b: "bar"}
such that obj returns {a:1, b: "bar"}. Or is this impossible and an new object has to be created?
Is there a native js way to extend an object such that the original object is modified?
eg
const obj = {a: 1}
const obj2 = {b: "bar"}
such that obj returns {a:1, b: "bar"}. Or is this impossible and an new object has to be created?
You can use Object.assign().
The
Object.assign()method is used to copy the values of all enumerable own properties from one or more source objects to a target object.
const obj = { a: 1 };
const obj2 = { b: 'bar' };
Object.assign(obj, obj2);
console.log(obj);