A-C > Array (object)

 

Array (object)

The Array object lets you access and manipulate arrays. An array is an object whose properties are identified by a number representing their position in the array. This number is referred to as the index. All arrays are zero-based, which means that the first element in the array is [0], the second element is [1], and so on. In the following example, myArray contains the months of the year.

myArray[0] = "January"
myArray[1] = "February"
myArray[2] = "March"
myArray[3] = "April"

To create an Array object, use the constructor new Array or the array access operator ([]). To access the elements of an array, use the array access operator ([ ]).

In Flash MX, the Array object has become a native object. As such, you will experience dramatic improvement in performance.

 
Method summary for the Array object

Method

Description

Array.concat

Concatenates the parameters and returns them as a new array.

Array.join

Joins all elements of an array into a string.

Array.pop

Removes the last element of an array and returns its value.

Array.push

Adds one or more elements to the end of an array and returns the array's new length.

Array.reverse

Reverses the direction of an array.

Array.shift

Removes the first element from an array and returns its value.

Array.slice

Extracts a section of an array and returns it as a new array.

Array.sort

Sorts an array in place.

Array.sortOn

Sorts an array based on a field in the array.

Array.splice

Adds and/or removes elements from an array.

Array.toString

Returns a string value representing the elements in the Array object.

Array.unshift

Adds one or more elements to the beginning of an array and returns the array's new length.


 
Property summary for the Array object

Property

Description

Array.length

Returns the length of the array.


 
Constructor for the Array object

Availability

Flash Player 5.

Usage

new Array()
new Array(length)
new Array(element0, element1, element2,...elementN)

Parameters

length An integer specifying the number of elements in the array. In the case of noncontiguous elements, the length parameter specifies the index number of the last element in the array plus 1.

element0...elementN A list of two or more arbitrary values. The values can be numbers, strings, objects, or other arrays. The first element in an array always has an index or position of 0.

Returns

Nothing.

Description

Constructor; lets you create an array. You can use the constructor to create different types of arrays: an empty array, an array with a specific length but whose elements have no values, or an array whose elements have specific values.

Usage 1: If you don't specify any parameters, an array with a length of 0 is created.

Usage 2: If you specify only a length, an array is created with length number of elements with no values.

Usage 3: If you use the element parameters to specify values, an array is created with specific values.

Example

Usage 1: The following example creates a new Array object with an initial length of 0.

myArray = new Array();

Usage 3: The following example creates the new Array object go_gos, with an initial length of 5.

go_gos = new Array("Belinda", "Gina", "Kathy", "Charlotte", "Jane");
trace(go_gos.join(" + "));

The initial elements of the go_gos array are identified as follows:

go_gos[0] = "Belinda";
go_gos[1] = "Gina";
go_gos[2] = "Kathy";
go_gos[3] = "Charlotte";
go_gos[4] = "Jane";

The following code adds a fifth element to the go-gos array and changes the first element:

go_gos[5] = "Donna";
go_gos[1] = "Nina"
trace(go_gos.join(" + "));

See also

Array.length, [] (array access)