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.

Related Articles:

17 Responsesto “How To: Making an independant copy of a JavaScript array”

  1. Kevin says:

    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

  2. rfinn says:

    DOESN’T WORK:
    tempArray = oldARray.slice();

    DOES WORK:
    tempArray = oldArray.slice(0);

  3. alain says:

    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

  4. korisu says:

    DOESN’T WORK:
    tempArray = oldARray.slice();

    DOES WORK:
    tempArray = oldArray.slice(0);

    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.)

  5. Brian Grayless says:

    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.

  6. Alex says:

    Brian-

    That doesn’t work in IE. If you’re going for cross-browser compatibility, you can’t use toSource().

  7. karthi says:

    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

  8. Allan says:

    So, is there a foolproof way of copying multidimensional arrays?

  9. Allan says:

    http://my.opera.com/GreyWyvern/blog/show.dml/1725165

    Aah, found this one. Works great! =)

  10. serge says:

    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;
    }

  11. Mitis says:

    Well if you have Mootools framework you can write:

    var newarray = $A(oldarray);

  12. Plamen says:

    var newArray = [].concat( oldArray);

  13. anne bebek ürünleri says:

    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

  14. louis vuitton imitation handbag says:

    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

  15. oregon virtual office says:

    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!

  16. http://dermexclusivereviews.weebly.com says:

    This could be a Shakeology Discount or a discount on fitness method like P90X.
    Many times when a player leaves a team, NFL jersey shops will place those particular NFL jerseys on sale to clear out their inventory.

    Your skin will feel soft and supple using Neutrogena’s.

  17. http://tiny6.com/l8nn says:

    Hello to every single one, it’s really a pleasant for me to go to see this web site, it includes
    valuable Information.

    My site – Kalkulator ([http://tiny6.com/l8nn](http://tiny6.com/l8nn “http://tiny6.com/l8nn”))

Leave a Reply

Line and paragraph breaks automatic.
XHTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>