I want to create a variable unrelated to an existing object but has the same value to the object initially. That means, if I change the new object, it will not affect the old value.
I've tried to use Object.assign({},oldObj)
, but it doesn't work if I push a value to the array item in the object. Here is my code:
let oldObj = {X:10, Y:[]};
let newObj = Object.assign({},oldObj);
newObj.Y.push(3.29231994);
console.log(oldObj);
Any help will be much appreciated!