Consider the following code
var myData = {name: 'John'};
function fixName(data) {
var newData = data;
newData.name = 'Mike';
return newData;
}
var myData2 = fixName(myData);
console.log(myData2.name);
console.log(myData.name);
‘Mike’ is printed twice because within my function, newData and myData point to the same thing. How can I alter my function so that it has no side effects upon myData? This means the program should print ‘Mike’ then ‘John’.
You need to clone that data object.
What is the best way to clone an object is answered here