How to fully copy / overwrite an object using Object.assign()? If there would be another approach, it would be highly appreciated. :) The purpose is to update the existing object to its new value.
Below is my code snippet and the expected result should be the same as the value of object2 only.
Expected result: { a: 8, b: 7 }
const object1 = {
a: 1,
b: 2,
c: {
d: 3
}
};
const object2 = {
a: 8,
b: 7
};
Object.assign(object1, object2);
console.log(object1);
object2and completely discard the existingobject1? You shouldn't useObject.assignfor thatobject1since it was declared withconst.