How To: Making an independant copy of a JavaScript array
In JavaScript, if you want to make an independent copy of an array, use the Array slice() function, as seen below
Code:
// 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.
Code:
// 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.


July 6th, 2006 at 4:06 am
I’ve tried using this method for a 2-d array but can’t get it to work.
I use: var newArray = oldArray.slice(); but each 1-d sub-array is not independant
I also tried:
var i=0;
var newArray=new Array();
for(i=0;i
September 8th, 2006 at 11:58 am
DOESN’T WORK:
tempArray = oldARray.slice();
DOES WORK:
tempArray = oldArray.slice(0);
October 18th, 2006 at 8:06 am
Beware of the following:
slice does not alter the original array, but returns a new “one level deep” copy that contains copies of the elements sliced from the original array. Elements of the original array are copied into the new array as follows:
* For object references (and not the actual object), slice copies object references into the new array. Both the original and new array refer to the same object. If a referenced object changes, the changes are visible to both the new and original arrays.
* For strings and numbers (not String and Number objects), slice copies strings and numbers into the new array. Changes to the string or number in one array does not affect the other array.
If a new element is added to either array, the other array is not affected.
source: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:slice
November 19th, 2006 at 4:26 pm
This is correct – the first argument (begin) of slice() does have to be specified, but if the second argument (end) is omitted, it slices from begin to the end of the array. Which means you’ll get the entire array if you slice from 0 onward.
(by the way, thanks! this was driving me bonkers over the weekend.)
July 20th, 2007 at 8:47 am
What I’ve found to be an even better solution that accommodates even multidimensional arrays is:
var new_arr = eval(orig_var.toSource());
This forces the original array to a string before evaluating into a new variable. It doesn’t matter how complex or nested the array/object is, it will make a new copy.
August 7th, 2007 at 5:09 pm
Brian-
That doesn’t work in IE. If you’re going for cross-browser compatibility, you can’t use toSource().
May 14th, 2008 at 1:28 am
Can you tell me how to duplication an array with in an array…
Its something like this,
arrCollege[0].arrClass[0].arrStudents
arrCollege is having arrClass which includes arrStudents…
I want to get the copy of the arrCollege in to a new array
Karthi
July 20th, 2008 at 6:16 am
So, is there a foolproof way of copying multidimensional arrays?
July 20th, 2008 at 6:25 am
http://my.opera.com/GreyWyvern/blog/show.dml/1725165
Aah, found this one. Works great! =)
August 14th, 2008 at 1:26 pm
I use this function, because slice does not garanty copies of array or arrays
function arrayCopy(t){
if(typeof t==’object’ && t.length>=0 && t.constructor==Array ){
var r=[];
r.push(arrayCopy(t);
return r;
}
return t;
}
October 28th, 2008 at 2:11 pm
Well if you have Mootools framework you can write:
var newarray = $A(oldarray);
April 18th, 2009 at 6:27 pm
var newArray = [].concat( oldArray);
April 15th, 2013 at 7:34 am
This is really interesting, You are an overly skilled blogger. I have joined your rss feed and stay up for seeking extra of your excellent post. Additionally, I’ve shared your web site in my social networks
April 27th, 2013 at 10:41 am
approximately 15鈥?at widest x 13鈥?tall at center x 6鈥?deep; weighs 2 lbs. Certificate of authenticity included You can get this from BlueFly.Dumonde Mid City Tote This
May 14th, 2013 at 11:31 pm
When I originally commented I clicked the “Notify me when new comments are added” checkbox
and now each time a comment is added I get several e-mails with the
same comment. Is there any way you can remove people from
that service? Thanks a lot!