Moscrif API Docs for: 2012q3
Show:

Array Class

Library: core

Represents indexed vector (array) of values. The Array object is used to store multiple values in a single variable.

Example:

    // An array can be defined in three ways.

    // 1.
    var myCars=new Array();
    myCars[0]="Saab";
    myCars[1]="Volvo";
    myCars[2]="BMW";

    //  2.
    var myCars=new Array("Saab", "Volvo", "BMW"); // condensed array

    // 3.
    var myCars=["Saab", "Volvo", "BMW"]; // literal array

Methods

this

(
  • [value]
)

Creates new array object from arguments provided.

Parameters:

  • [value] Object optional multiple

Example:

var myCars=new Array("Saab", "Volvo", "BMW");

clone

() Array

Returns brand new copy of the array.

Returns:

Array: New sliced array.

concat

(
  • [value]
)
Array

Appends array by values.

Parameters:

  • [value] Object optional multiple

Returns:

Array: The modified array

filter

(
  • callback
  • [thisObject]
)
Array

This method calls the callback function once for each element in the array, and constructs new array of all the values for which the callback returns true. The filter method does not mutate the array on which it is called.

Parameters:

  • callback Function

    he callback function may accept following parameters: callback(currentValue, index, array) where:

            currentValue is a value of array element at the index position;
            array is the array to what this method applied.
    
  • [thisObject] Object optional

    Function uses object as this

Returns:

Array: New array with changed values.

Example:

var filtered = [12, 5, 8, 130, 44].filter(:el: el > 10);
// filtered is [12, 130, 44] here

indexOf

(
  • value
  • [notfound=-1]
)
Array

Find value in the array.

Parameters:

  • value Object

    Value for searching

  • [notfound=-1] Object optional

    Value, which is returned if required value was not found

Returns:

Array: Returns index of searching value or value set by the second parameter (default: -1) if nothing was found.

join

(
  • delimiter
)
String

Creates string fram all array's elements separated by the delimeter. If delimiter is not specified, the default is comma.

Parameters:

  • delimiter String

    Character used to separat array's items.

Returns:

String: String with all elements of the array separated by the delimeter.

lastIndexOf

(
  • value
  • [notfound=-1]
)
Array

Get last index of the value in array. If the value is not found returns the notfound value (-1 by default).

Parameters:

  • value Object

    Value for searching

  • [notfound=-1] Object optional

    Value, which is returned if required value was not found

Returns:

Array: Returns index of searching value or value set by the second parameter (default: -1) if nothing was found

map

(
  • callback
  • [thisObject]
)
Array

This method creates a new array with the result of calling a provided callback function on every element in this array. If thisObject is provided then the callback will have it as this. The map method does not mutate the array on which it is called.

Parameters:

  • callback Function

    The callback function may accept following parameters: callback(currentValue, index, array) where currentValue is a value of array element at the index position and array is the array to what this method applied.

  • [thisObject] Object optional

    Function uses object as this

Returns:

Array: New array with changed values.

Example:

function square(currentValue, index, array)
{
    // calculate square for every value
    return currentValue * currentValue;
}

var numbers = new Array(4,6,8);
// apply square function to all array items
var squared = numbers.map(square);
// get changed values
console<<squared.toString()<<"\n"; // 16,36,64
// original values in array numbers ​​are unchanged
console<<numbers.toString()<<"\n"; // 4,6,8

push

(
  • [value]
)
Object

Appends array by values.

Parameters:

  • [value] Object optional multiple

Returns:

Object: Last inserted element

push

() Object

Removes last element from array. This affects the length of the array!

Returns:

Object: Removed element or undefined

reduce

(
  • callback
  • [initialValue]
)
Array

This method applies the function simultaneously against two values of the array (from left-to-right) as to reduce it to a single value. The method returns result of last callback invocation. The method does not mutate the array on which it is called.

Parameters:

  • callback Function

    The callback function may accept following parameters: callback(previousValue, currentValue, index, array) where:

            previousValue is either initialValue (on first run) or result of previous call of the callback function;
            currentValue is a value of array element at the index position;
            array is the array being scanned (reduced).
    
  • [initialValue] Object optional

    InitialValue value

Returns:

Array: New array with changed values.

remove

(
  • index
)
Object

Removes element at index.

Parameters:

  • index Integer

    Index of element to remove.

Returns:

Object: Removed element or undefined if element was not found.

Example:

var myCars = new Array("Saab", "Volvo", "BMW", "Mitsubishi");
console<<myCars[0..3]<<"\n";        // Saab, Volvo, BMW
console<<myCars.remove(1)<<"\n";    // Volvo
console<<myCars[0..3]<<"\n";        // Saab, BMW, Mitsubishi

removeByValue

(
  • index
)
Object

Tries to locate element by value in the array and removes it.

Parameters:

Returns:

Object: Removed element or undefined if element was not found.

reverse

() Array

Reverses order of elements in the array in-place. Returns the array.

Returns:

Array: The array.

shift

() Object

Removes first element of the array. This method changes the length of an array!

Returns:

Object: Removed element or undefined

Example:

console<<myCars[0..3]<<"\n";  // Saab,Volvo,BMW
console<<myCars.shift()<<"\n"; // Saab
console<<myCars[0..3]<<"\n";  // Volvo,BMW,Mitsubishi

slice

(
  • start
  • [end]
)
Array

Create new array consisting from elements of the array from start up to but not including end index, order between start and end (not included) index.

Parameters:

Returns:

Array: New sliced array.

Example:

var myCars=new Array("Saab", "Volvo", "BMW", "Mitsubishi");
console<<myCars[0..4]<<"\n";        // Saab, Volvo, BMW, Mitsubishi
console<<myCars.slice(1)<<"\n";     // Volvo, BMW, Mitsubishi
console<<myCars[0..4]<<"\n";        // Saab, Volvo, BMW, Mitsubishi
console<<myCars.slice(1, 3)<<"\n";  // Volvo, BMW
console<<myCars[0..4]<<"\n";        // Saab, Volvo, BMW, Mitsubishi

sort

(
  • [sortFunction]
)
Array

Sorts elements of the array in ascending order, if the sortFunction provided is used for comparing elements during sort.

Parameters:

  • [sortFunction] Function optional

    Function used for sorting. Items shall accept two values in parameters and return -1, 0 or +1 as result.

Returns:

Array: Sorted array.

Example:

function sortFunction (firstItem, secondItem)
{
    if (firstItem.length == secondItem.length) {
        // both items are same
        return 0;
    } else {
        if (firstItem.length &lt; secondItem.length) {
            // the first value should be before second
            return -1;
        } else {
             // the first value should be be after second
            return 1;
        }
    }
}

var myCars=new Array("Saab", "Volvo", "BMW", "Mitsubishi");
myCars.sort(); // default: sort by alphabetical order
console<<myCars[0..4]<<"\n";  // BMW,Mitsubishi,Saab,Volvo
myCars.sort(sortFunction); // sort by length of string (from shortest to longest)
console<<myCars[0..4]<<"\n";  // BMW,Saab,Volvo,Mitsubishi

toString

() String

Create comma separated string from array.

Returns:

String: comma separated list of values

unshift

(
  • value
)
Object

Inserts a value at first array position.

Parameters:

  • value Object

    Object, which should be added to the array.

Returns:

Object: Inserted value.

Example:

var myCars=new Array("Saab", "Volvo", "BMW", "Mitsubishi");
console<<myCars[0..3]<<"\n";  // Saab,Volvo,BMW
console<<myCars.unshift("Jaguar")<<"\n"; // Jaguar
console<<myCars[0..3]<<"\n";  // Jaguar,Saab,Volvo

valueOf

() String

Same as toString - creates comma separated string from array.

Returns:

String: Comma separated list of values

Properties

[begin..end]

Object

Returns part of vector and contains elements from beginning index and up to or excluded end index. Begin or/and end are optional. If begin is ommited then it is assumed to be 0, if end - length is used as end value.

Example:

var myCars=new Array("Saab", "Volvo", "BMW", "Mitsubishi");
console<<myCars[1..3]<<"\n";  // Volvo,BMW

[index]

Object var myCars=new Array("Saab", "Volvo", "BMW"); console<<"\n"; // Volvo

Refers to a particular element of the array at the index position. Index is zero-based.

length

Integer

Get number of items in array.

Example:

var myCars=new Array("Saab", "Volvo", "BMW");
console<<myCars.length<<"\n";  // 3