A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: How do I get a copy of an array??

  1. #1
    Junior Member
    Join Date
    Jun 2001
    Posts
    9
    Why isnt it possible to copy an array with the statement:
    var parse_array=new Array();
    parse_array=post_array;

    It seem that I only get a referance to my orginal array. Cus when Im perfoming operations on the new array, I actually change the data stored in the orginal. I want to keep that array unchanged. Is it possible to tell Flash that I dont want an reference to to the orginal array?
    (post_array is a global variable whilst parse_array is a local).

    All help are appriciated!


  2. #2
    Senior Member
    Join Date
    Nov 2000
    Posts
    106
    Found a work around using slice.

    Here's the test code I used:

    Code:
      var post_array=new Array();
      post_array[0]="test0";
      post_array[1]="test1";
      trace("post_arrayA: " + post_array.join());
    
      var parse_array=new Array(); 
      parse_array=post_array.slice(0, post_array.length); 
      trace("parse_arrayA: " + parse_array.join());
    
      parse_array[0]="newTest0";
      trace("post_arrayB: " + post_array.join());
      trace("parse_arrayB: " + parse_array.join());
    Outputs:

    post_arrayA: test0,test1
    parse_arrayA: test0,test1
    post_arrayB: test0,test1
    parse_arrayB: newTest0,test1



    Not sure if this is the tidiest method but it works.

    G

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center