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[0].a = 10;

Here we will be creating new references for each object.


JS Enthusiast, with a passion for travelling and writing.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store