SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Copy array?

  1. #1
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Copy array?

    I have a multi-dimension array that I want to copy.

    Doing new = old creates a reference, and new = old.slice() only creates a one-level deep copy.

    How can I copy my ENTIRE array?
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    Array.prototype.copy=function(){
    	var A=[];
    	var L=this.length;
    	for(var i=0;i<L;i++){
    		A.push(this[i]);
    	}
    	return A;
    }
    Eg:

    Code:
    var A=[1,2,3,[4,5,6]];
    var B=A.copy();
    alert(B.join('\n') ) /* returns:
                                    1
                                    2
                                    3
                                    4, 5, 6
    */

  3. #3
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is really no need to extend the Array object to copy an array. Do this instead
    Code:
    var oldArray= [1,2,3,[4,5,6]];
    var newArray = oldArray.slice();
    
    // Add element to newArray, oldArray should not get this value
    newArray.push(7);
    
    // Check that the above statement is true
    alert(newArray.length); // 5
    alert(oldArray.length); // 4

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mrhoo's approach is correct but he forgot the recursion:

    Code:
    Array.prototype.copy = function() {
    	var a = [];
    	for(var i = 0; i < this.length; i++)
    		a.push(this[i].copy ? 
    			this[i].copy() : this[i]);
    	return a;
    }
    Test

    Code:
    a = [1, [21, 22], 3];
    b = a.copy()
    b[0] = "foo"
    alert(a[0]) // should be 1
    b[1][0] = "bar"
    alert(a[1][0]) // should be 21

  5. #5
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right you are, stereo-
    but to be complete, we ought to include arrays that have
    objects for members:

    Code:
    Array.prototype.copy=function(){
        var A=[], wot;
        var L=this.length;
        for(var i=0;i<L;i++){
            var tem=this[i];
            wot=tem.constructor;
            if(wot){
                if(wot==Array || wot==Object){
                    A.push(new wot)
                    for(var p in tem){
                        A[i][p]=(tem[p].copy? tem[p].copy():tem[p]);
                    }
                    continue;
                }
            }
            A.push(tem);
        }
        return A;
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •