Abhimanyu Chauhan
1 min readJun 24, 2019

--

I would say this is a partial solution, as objects {b:2} and {c:3} are still pointing to the same reference. Another issue with the solution is that it will affect the bindings, if you are using any JS framework to display data.

Try the below solution:

function addToArray(arrayRef,value){
var newArray = arrayRef.map(val=>{
return Object.assign({},val)
});
newArray.push(value);
newArray[0].a = 10;
console.log(newArray);
}

Here we will be creating new references for each object.

--

--

Abhimanyu Chauhan
Abhimanyu Chauhan

Written by Abhimanyu Chauhan

JS Enthusiast, with a passion for travelling and writing.

No responses yet