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

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -