javascript - Modifying one array modifies another one I made from it -
this question has answer here:
- copying array value in javascript 19 answers
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
Post a Comment