Arrays

The topics are:

Script Arrays

Arrays provide a way of manipulating ordered sets of values referenced through an index starting from 0. Unlike arrays in other languages, Script arrays do not have a fixed size and are automatically expanded as new elements are added. For example, in the following program, an array is created empty, and is then added new elements:

a = new Array() // Create an empty array

a[0] = "first" // Set the element 0

a[1] = "second" // Set the element 1

a[2] = "third" // Set the element 2

Arrays are internally represented as sparse objects, which means that an array where only the element 0 and the element 10000 have been set occupies just enough memory to store these two elements, not the 9999 which are between 0 and 10000.

Array Constructor

The array constructor has two distinct syntaxes:

Script Array Constructor Syntax

Syntax

Effect

new Array(length)

Returns a new array of length length with its elements from 0 to length-1 set to The null Value .

If length is not a number, and its Automatic Conversion to a Number yields NaN, the second syntax is used.

Examples:

  new Array(12)-> an array a with length 12 and a[0] to a[11] containing null.

  new Array("5") -> an array a with length 5 and a[0] to a[4] containing null.

  new Array("foo") -> see second syntax.

new Array(element1, ..., elementn)

Returns a new array a of length n with a[0] containing element1, a[1] containing element2, and so on. If no argument is given, that is n=0, an empty array is created. If n=1 and element1 is a number or can be converted to a number, the first syntax is used.

Examples:

  new Array(327, "hello world") -> an array a of length 2 with a[0] == 327 and a[1] == "hello world".

  new Array() -> an array with length 0.

  new Array("327") -> see first syntax.

Array Properties

The array properties are:

Script Array Properties

Syntax

Effect

array[index]

If index can be converted to a number between 0 and 2e32‑2 (see Automatic Conversion to a Number ), array[index] is the value of the indexth element of the array. Otherwise, it is considered as a standard property access.If this element has never been set, null is returned.

Example: Suppose that the array a has been created with:

  a = new Array("foo", 12, true)

Then:

  a[0] -> "foo"

  a[1]-> 12

  a[2]-> true

  a[3]-> null

  a[1000]-> null

When an element of an array is set beyond the current length of the array, the array is automatically expanded:

  a[1000] = "bar"
  // the array is automatically expanded.

Unlike other properties, the numeric properties of an array are not listed by the for..in statement.

array.length

The length of array, which is the highest index of an element set in array, plus one. It is always included in 0 and 2e31‑1. When a new element is set in the array, and its index is greater or equal to the current array length, the length property is automatically increased.

Example: Suppose that the array a has been created with:

  a = new Array("a", "b", "c")

Then:

  a.length-> 3

  a[100] = "bar"; a.length-> 101

You can also change the length of an array by setting its length property.

  a = new Array(); a[4] = "foo"; a[9] = "bar";

  a.length -> 10

  a.length = 5

  a.length-> 5

  a.length-> 5

  a[4]-> "foo"

  a[9]-> null

Array Methods

Arrays have the following methods:

Script Array Methods

Syntax

Effect

array.join( [ separator ] )

Returns a string which contains the elements of the array converted to strings, concatenated together and separated with separator. If separator is omitted, it is taken to be ",". Elements which are not initialized are converted to the empty string. See also the string method split.

Example: Suppose that the array a has been created with

  a = new Array("foo", 12, true)

Then:

  a.join("//") -> "foo//12//true"

  a.join()-> "foo,12,true"

array.sort( [ function ] )

Sorts the array. The elements are sorted in place; no new array is created.

If function is not provided, array is sorted lexicographically: Elements are compared by converting them to strings and using the <= operator. With this order, the number 20 would come before the number 5, since "20" < "5" is true.

If function is supplied, the array is sorted according to the return value of this function. This function must take two arguments x and y and return:

-1 if x is smaller than y;

0 if x is equal to y;

1 if x is greater than y.

Example: Suppose that the function compareLength is defined as

  function compareLength(x, y) {
    if (x.length < y.length) return -1;
    else if (x.length == y.length) return 0;
    else return 1;
  }

and that the array a has been created with:

  a = new Array("giraffe", "rat", "brontosaurus")

Then a.sort() will reorder its elements as follows:

  "brontosaurus" "rat" "giraffe"

while a.sort(compareLength) will reorder them as follows:

  "rat" "giraffe" "brontosaurus"

array.reverse( )

Transposes the elements of the array: The first element becomes the last, the second becomes the second to last, etc. The elements are reversed in place; no new array is created.

Example: Suppose that the array a has been created with

a = new Array("foo", 12, "hello", true, false)

Then a.reverse() changes a so that:

a[0]-> false

a[1] -> true

a[2] -> "hello"

a[3]-> 12

a[4]-> "foo"

array.toString( )

Returns the string "[object Object]".