You must encode / decode JSON to prevent a shallow copy. To learn more about deep topography and shallow copy, here is the link. What is the difference between a deep copy and a shallow copy?
How var root = JSON.parse (JSON.stringify (root)); is the wrong way to prevent a shallow copy, you can use the jquery clone method or just the javascripts slice method for the javascript deepcopy array.
eg.
var d=[1,2,3,4,5,6];//create array var b=d;//copy array into another array (shallow copy/reference copy) var e=d.slice();//clone array in another variable (deep copy) d[0]=9; //change array element console.log(d)// result : [9,2,3,4,5,6] (changed array) console.log(b)// result : [9,2,3,4,5,6] (changed array due to reference) console.log(e)// result : [1,2,3,4,5,6] (unchanged array due to deep copy)
Another solution is to use underscores. If you do not need the full javascript underscore code, you can select the cloning part in the underscore library.
In underlining, you can clone an array of objects using:
var a = [{f: 1}, {f:5}, {f:10}]; var b = _.map(a, _.clone);
He will print
[{"f":1},{"f":5},{"f":10}]