How To: Making an independant copy of a JavaScript array
Wednesday, August 31st, 2005In JavaScript, if you want to make an independent copy of an array, use the Array slice() function, as seen below
// JavaScript syntax
var landArray = new Array("Asia", "Africa");
var continentArray = landArray;
landArray.push("Australia"); // this also adds "Australia" to continentList
To create a copy of an array that is independent of another array:
Use the Array object’s slice() method.
For example, the following statements create an array and then use slice() to make an independent copy of the array.
// JavaScript syntax
var oldArray = ["a", "b", "c"];
var newArray = oldArray.slice(); // makes an independent copy of oldArray
After newArray is created, editing either oldArray or newArray has no effect on the other.
Save you time and effort to make an independant copy.



