I'm fairly new to Javascript Object. I've defined a non-destructive function to update an object like this:
function updateObjectWithKeyAndValue(object, key, value) {
return Object.assign({}, object, { key: value });
}
let object = {a: 1};
console.log(updateObjectWithKeyAndValue(object, 'b', 2));
console.log(object);
I'm getting the return value of the function as { a: 1, key: 2 } instead of { a: 1, b: 2 }. Is there something I'm not doing right? Thanks...