Skip to main content
May 4, 2011
Answered

Duplicate array

  • May 4, 2011
  • 1 reply
  • 1376 views

Hello,

Please can anyone tell me the best way to duplicate an array and then push it into itself ??

Do I need to use concat or is there a way I can keep the original array ?

Thanks in advance

This topic has been closed for replies.
Correct answer kglad

my code is just fine, but I would like to create 96 tiles instead of 48..

Two of each instance.


if you want 96 tiles, increase one (or both) of your for-loops so you create 96 tiles.  that has nothing to do with array duplication.

arrays are variables.  their elements point to objects but are not the actual objects.  if you want more objects, create them.  if you want more object references, duplicating array elements or an array might make sense.

1 reply

kglad
Community Expert
Community Expert
May 4, 2011

use slice():

var duplicate_array:Array=original_array.slice();

and changes to duplicate_array will not affect original_array (and vice-versa).

May 4, 2011

so after this I could just use

for(var i = 0; i <= duplicate_array.length; i++) {

     original_array.push(duplicate_array);

}

to push the duplicated array into the original one ??

kglad
Community Expert
Community Expert
May 4, 2011

you could but you probably don't want to do that.

what are you trying to accomplish?  are you trying to double the length of original_array by duplicating each element of the array?