Code Snippets: How to duplicate an Array in ActionScript 3 (AS3)

Array duplication or copying is actually a very simple task with multiple methods of the Array class returning a new Array. The only drawback is that their is no clone, copy, or duplicate method that would make it obvious to first time users. Instead the Array class has the concat and slice function that can be used to return a new Array with all the same entries as the first.

Concat method

The concat method concatenates the parameters passed to the array and returns the new array with all the entries. Therefore if you pass nothing into the function you will be returned with a duplicate of the original.

var array:Array = new Array(1, 2, 3);
var copyArray:Array = array.concat();

Slice method

The slice method is used to return the values in an array from the start index to the end index without modifying the original array. Much like concat if you pass no arguments you will be returned with an array containing all the values of the first.

var array:Array = new Array(1,2,3);
var copyArray:Array = array.slice();


// Code Snippets // Flash //

Comments & Questions

Add Your Comment