Array

Lets you work with arrays.

Created by

The Array object constructor:

new Array( arrayLength )
new Array( element0, element1, ..., elementN )

An array literal:

[element0, element1, ..., elementN]

Parameters

Parameter Description
arrayLength The initial length of the array. You can access this value using the length property. If the value specified is not a number, an array of length 1 is created, with the first element having the specified value. The maximum length allowed for an array is 4,294,967,295.
elementN A list of values for the array's elements. When this form is specified, the array is initialized with the specified values as its elements, and the array's length property is set to the number of arguments.

Description

An array is an ordered set of values associated with a single variable name.

The following example creates an Array object with an array literal; the coffees array contains three elements and a length of three:

You can construct a dense array of two or more elements starting with index 0 if you define initial values for all elements. A dense array is one in which each element has a value. The following code creates a dense array with three elements:

Indexing an array

You index an array by its ordinal number. For example, assume you define the following array:

You then refer to the first element of the array as myArray[0] and the second element of the array as myArray[1].

Specifying a single parameter

When you specify a single numeric parameter with the Array constructor, you specify the initial length of the array. The following code creates an array of five elements:

The behavior of the Array constructor depends on whether the single parameter is a number.

If the value specified is a number, the constructor converts the number to an unsigned, 32-bit integer and generates an array with the length property (size of the array) set to the integer. The array initially contains no elements, even though it might have a non-zero length.

If the value specified is not a number, an array of length 1 is created, with the first element having the specified value.

The following code creates an array of length 25, then assigns values to the first three elements:

Increasing the array length indirectly

An array's length increases if you assign a value to an element higher than the current length of the array. The following code creates an array of length 0, then assigns a value to element 99. This changes the length of the array to 100.

Creating an array using the result of a match

The result of a match between a regular expression and a string can create an array. This array has properties and elements that provide information about the match. An array is the return value of RegExp.exec, String.match, and String.replace. To help explain these properties and elements, look at the following example and then refer to the table below:

The properties and elements returned from this match are as follows:

Property/Element Description
input A read-only property that reflects the original string against which the regular expression was matched.
index A read-only property that is the zero-based index of the match in the string.
[0] A read-only element that specifies the last matched characters.
[1], ...[n] Read-only elements that specify the parenthesized substring matches, if included in the regular expression. The number of possible parenthesized substrings is unlimited.

For example:

Property Summary

Property Description
constructor Specifies the function that creates an object's prototype.
index For an array created by a regular expression match, the zero-based index of the match in the string.
length Reflects the number of elements in an array.
prototype Allows the addition of properties to all objects.

Method Summary

Method Description
concat Joins two arrays and returns a new array.
join Joins all elements of an array into a string.
pop Removes the last element from an array and returns that element.
push Adds one or more elements to the end of an array and returns the new length of the array.
reverse Transposes the elements of an array: the first array element becomes the last and the last becomes the first.
shift Removes the first element from an array and returns that element.
slice Extracts a section of an array and returns a new array.
splice Adds and/or removes elements from an array.
sort Sorts the elements of an array.
toSource Returns an array literal representing the specified array; you can use this value to create a new array. Overrides the Object.toSource method.
toString Returns a string representing the array and its elements. Overrides the Object.toString method.
unshift Adds one or more elements to the front of an array and returns the new length of the array.
valueOf Returns the primitive value of the array. Overrides the Object.valueOf method.

In addition, this object inherits the Object.watch and Object.unwatch methods from Object.

Examples

Example 1. The following example creates an array, msgArray, with a length of 0, then assigns values to msgArray[0] and msgArray[99], changing the length of the array to 100.

Example 2: Two-dimensional array. The following code creates a two-dimensional array and assigns the results to myVar.

This example assigns the following string to myVar (line breaks are used here for readability):

See Also

Array (MetaScript Guide) Labels: object, arrays

Labels

object object Delete
arrays arrays Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.