Array Values


Methods available on array values.


arr.Add( val: * ): void

Appends the given value to the end of the array.


arr.Apply( fn: function ): void

For each element of the array, call fn(element) and replace the original element with the return value of the function.


arr.Clear(): void

Remove all elements from the array.


arr.Filter( passFunc: function ): array

Creates a new array including only those elements which are permitted by the passFunc filter function. For each element passFunc(index,value) is called, if it returns true the value is included in the new array. Otherwise the element is omitted.


arr.Find( value ): int|null

Performs a linear search through the array for the given value. If found, returns the index of the element, otherwise it returns null.


arr.InsertAt( index: int, val: * ): void

Insert a value at the given index in the array.


arr.Join( other: array ): void

Add all elements from another array onto the end of this one.


arr.Length(): int

Returns the number of elements in the array.

Map fn: function ): array

Creates a new array of the same length as this array. Each value in this array is passed as a parameter to function fn and the return value is assigned to replace the element in the new array.


arr.Pop(): *

Remove an element from the end of the array and return it.


arr.Push( val: * ): void

Appends the given value to the end of the array. Just like Add does.


arr.Reduce( fn: function ): *

Reduce an array down to a single value. For each element in the array, invoke the function fn(previous, current) where previous is the result of the previous call and current is the current element being operated on. For a zero-length array, Reduce returns null. For length 1, it simply returns the first element.

For arrays of length 2 or more, the function is called with the first two elements as initial parameters. The next iteration then uses the result of that function call, plus the value of the third element as the parameters to fn, and so on. The final result is returned to the caller.


arr.RemoveAt( index: int ): void

Remove the value in the array at the given index.


arr.Reverse(): void

Reverse all elements of the array in-place.


arr.Slice( start: int, [end: int] ): string

Returns a sub-section of the array starting at start and continuing until the element before end. If end is omitted the slice will contain the whole of the remainder of the array. If start is negative the index is calculated as length + start. If end is negative the index is calculated as length + end.


arr.Sort( [compare: function] ): void

Sort the array. If using the optional compare function, it must conform to the prototype:

function custom_compare( a, b )
    if ( a > b ) {
        return 1;
    } else if ( a < b ) {
        return -1;
    } else {
        return 0;

Alternatively, a more compact implementation would be to use the <=> operator and a lambda expression:

arr.sort( @(a,b) a <=> b );