View comments | RSS feed

concat (Array.concat method)

public concat([value:Object]) : Array

Concatenates the elements specified in the parameters with the elements in an array and creates a new array. If the value parameters specify an array, the elements of that array are concatenated, rather than the array itself. The array my_array is left unchanged.

Availability: ActionScript 1.0; Flash Player 5

Parameters

value:Object [optional] - Numbers, elements, or strings to be concatenated in a new array. If you don't pass any values, a duplicate of my_array is created.

Returns

Array - An array that contains the elements from this array followed by elements from the parameters.

Example

The following code concatenates two arrays:

var alpha_array:Array = new Array("a","b","c");
var numeric_array:Array = new Array(1,2,3);
var alphaNumeric_array:Array =alpha_array.concat(numeric_array); 
trace(alphaNumeric_array);
// Creates array [a,b,c,1,2,3].

The following code concatenates three arrays:

var num1_array:Array = [1,3,5];
var num2_array:Array = [2,4,6];
var num3_array:Array = [7,8,9];
var nums_array:Array=num1_array.concat(num2_array,num3_array) 
trace(nums_array);
// Creates array [1,3,5,2,4,6,7,8,9].

Nested arrays are not flattened in the same way as normal arrays. The elements in a nested array are not broken into separate elements in array x_array, as shown in the following example:

var a_array:Array = new Array ("a","b","c");

// 2 and 3 are elements in a nested array.
var n_array:Array = new Array(1, [2, 3], 4); 

var x_array:Array = a_array.concat(n_array);
trace(x_array[0]); // a
trace(x_array[1]); // b
trace(x_array[2]); // c
trace(x_array[3]); // 1
trace(x_array[4]); // 2, 3 
trace(x_array[5]); // 4

Version 8

Comments


Robotron2084 said on May 12, 2006 at 9:21 PM :
Its also useful to note that if you need to copy an array, you may do so with concat by creating a new array and calling concat on the old array.:

var foo = ["a","b","c"];
var bar = new Array().concat(foo);

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/8/main/00001904.html