javascript - Modifying one array modifies another one I made from it -


this question has answer here:

var allcoords = [{ x: 2, y: 3 }, { x: 3, y: 4 }]; var emptyspaces = allcoords;  emptyspaces.splice(0, 1); console.log(allcoords.length); console.log(emptyspaces.length); 

i not understand why both of these output "1". why original array, allcoords being modified, when want second one, emptyspaces edited?

modify code this:

(function testing() {     var allcoords = [];     var emptyspaces = allcoords.slice();      emptyspaces.push({         x: 1,         y: 2     });     console.log(allcoords.length);     console.log(emptyspaces.length); }()); 

Comments

Popular posts from this blog

javascript - How do I find how many occurences are there of a highlighted string, and which occurence is it? -

javascript - How to synchronize the Three.js and HTML/SVG coordinate systems (especially w.r.t. the y-axis)? -

java - Reading data from multiple zip files and combining them to one -