Developer Resources

Array

Overview

An array class for storing lists of objects or data.

Base Class

Object

Constructor

Array()
Array(length : Integer)
Array(elem_0 : Object, ... , elem_n : Object)

Arguments

length
The index of the highest element of array plus one. Only the number of elements in the Array if every index smaller than the highest index has an element.
elem0, ..., elemn
The first n elements of the new Array. n has to greater than 1.

Methods

Array.concat
Concatenates a number of elements to an Array.
Array.join
Returns the elements of an Array as a string.
Array.pop
Deletes and returns the last element of an Array.
Array.push
Appends a number of elements to the end of an Array.
Array.reverse
Reverses the order of elements in an Array.
Array.shift
Deletes and returns the first element of an Array.
Array.slice
Returns a selection of elements of an Array in a new Array.
Array.sort
Sorts the elements of an Array.
Array.splice
Returns and replaces a selection of elements of an Array in a new Array with other elements.
Array.toLocaleString
Returns a local specific string representation of an Array.
Array.toString
Returns a string representation of an Array.
Array.unshift
Prepends a number of elements to the beginning of an Array.

Inherited Methods

Object.hasOwnProperty
Checks for the existence of an object property
Object.toLocaleString
Returns a string that represents the object.
Object.toString
Returns a string that represents the object.
Object.valueOf
Returns the 'primitive' value of the object.

Example

// creating a simple array
var arr = new Array;

arr[0] = "aaa";
arr[1] = "bbb";
arr[2] = "ccc";

for (var i in arr)
{
    alert("element " + i +" is " + arr[i]);
}


// sort an array of strings
var array_of_strings = new Array("Finch",
                                 "Bluebird",
                                 "Goose",
                                 "Sparrow",
                                 "Hummingbird",
                                 "Duck");

array_of_strings.sort();
alert(array_of_strings);


// sort an array of numbers -- this requires
// writing a sort routine of our own, because
// by default the sort() method sorts as if
// all elements were strings

function numericSort(a, b)
{
    return (a < b ? -1 : 1);
}

var array_of_numbers = new Array(5,4,10,50,45,52,1,12);

array_of_numbers.sort(numericSort);
alert(array_of_numbers);

Array.concat

function Array.concat(elem_n+1 : Object, ..., elem_n+m : Object) : Array(Object)

Arguments

elem_n+1, ..., elem_n+m
The elements to concatenate to the array. If a specified element in the parameter list is an array, the contents of the specified array are copied into the result.

Returns

Returns a new Array, created by concatenating the input elements to the array.

Description

Returns a new array, without modifying the original array, formed by concatenating the input elements with the elements of the array.

Array.join

function Array.join() : String
function Array.join(separator : String) : String

Arguments

separator
A string that will be used to separate the elements of the Array in the returned String.

Returns

Returns a string formed by converting all elements of array to strings and concatenating them with the separator added between elements. If a separator is not supplied, a "," is used.

Description

Joins the elements of the array into a string, where the individual elements of the array are separated by separator if it is specified, or by "," if the separator is not specified.

Array.pop

function Array.pop() : Object

Returns

Returns the last element of the array if the array is not empty, otherwise returns undefined.

Description

Deletes and returns the last element of the array. If the array is empty, the function returns undefined and leaves the array unchanged.

Array.push

function Array.push(elem_n+1 : Object, ..., elem_n+m : Object) : Integer

Arguments

elem_n+1, ..., elem_n+m
The objects to append to the array.

Returns

The new length of the array.

Description

Appends a number of elements to the end of an Array.

Array.reverse

function Array.reverse()

Description

Reverses the order of the elements of the array.

Array.shift

function Array.shift() : Object

Returns

Returns the first element of the array.

Description

Deletes and returns the first element of the array and moves all the other elements down one place. If the array is empty, the function returns undefined and does not modify the array.

Array.slice

function Array.slice(start : Integer) : Array(Object)
function Array.slice(start : Integer, end : Integer) : Array(Object)

Arguments

start
The index of the element that will be the first element of the returned array.
end
The index of the first element that will not be included in the returned array.

Returns

A new array containing all elements of the array from start to end, including the start element, but excluding the end element.

Description

Returns a new array containing all elements of array with an index greater or equal than start and less than end in order. If start or end is negative, the result of adding this negative number to the length of the array is used. If end is not specified, all elements from the start to the end of the array are used. Note: slice() does not modify array.

Array.sort

function Array.sort() : Array(Object)
function Array.sort(compare_function : Function) : Array(Object)

Arguments

compare_function
A comparison function to be used for sorting. This function should take two arguments and return 1) a value less than zero if the first argument should appear before the second argument in the returned Array, 2) zero if it doesn't matter in which way the arguments appear in the returned Array, and 3) a positive number otherwise.

Returns

Returns the original array with the elements sorted.

Description

Sorts the array and returns it using the compare_function function. If compare_function is not specified, the elements are converted to Strings and sorted according to the character encoding. Undefined elements will appear at the end of the array.

Array.splice

function Array.splice(start : Integer) : Array(Object)
function Array.splice(start : Integer, length : Integer) : Array(Object)
function Array.splice(start : Integer, length : Integer, newElem_1 : Object, ..., newElem_k : Object) : Array(Object)

Arguments

start
The index of the first element that is to be replaced.
length
The number of elements that will be deleted from the array.
newElem_1, ..., newElem_k
Values that will be inserted into the array.

Returns

A new array containing the elements deleted from the array.

Description

Deletes and returns the length elements of the array after start, including the element at start. If length is not supplied, all elements after start will be deleted. If newElem_1, ..., newElem_k are supplied, they will be inserted at start after the deletion.

Array.toLocaleString

function Array.toLocaleString() : String

Returns

Returns a localized string that represents the array.

Description

Calls the function toLocaleString() on every element of the array and returns the results concatenated with a locale-specific separator.

Array.toString

function Array.toString() : String

Returns

Returns a string that represents the array.

Description

Converts each element of the array to a string, concatenates the elements using the "," as a separator, and returns a single concatenated string of the elements.

Array.unshift

function Array.unshift(newElem_1 : Object, ..., newElem_k : Object) : Integer

Arguments

newElem_1, ..., newElem_k
The values to insert at the beginning of the array.

Returns

Returns the new length of the array.

Description

Inserts newElem_1, ..., newElem_k at the beginning of array, shifts other elements forward accordingly, and returns the new length of the array.