Array Class
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
Item Index
Methods
Properties
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:
concat
[value]
Appends array by values.
Parameters:
-
[value]
Object optional multiple
Returns:
filter
callback
[thisObject]
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
Functionhe 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 optionalFunction uses object as this
Returns:
Example:
var filtered = [12, 5, 8, 130, 44].filter(:el: el > 10);
// filtered is [12, 130, 44] here
indexOf
value
[notfound=-1]
Find value in the array.
Parameters:
-
value
ObjectValue for searching
-
[notfound=-1]
Object optionalValue, which is returned if required value was not found
Returns:
join
delimiter
Creates string fram all array's elements separated by the delimeter. If delimiter is not specified, the default is comma.
Parameters:
-
delimiter
StringCharacter used to separat array's items.
Returns:
lastIndexOf
value
[notfound=-1]
Get last index of the value in array. If the value is not found returns the notfound value (-1 by default).
Parameters:
-
value
ObjectValue for searching
-
[notfound=-1]
Object optionalValue, which is returned if required value was not found
Returns:
map
callback
[thisObject]
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
FunctionThe 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 optionalFunction uses object as this
Returns:
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]
Appends array by values.
Parameters:
-
[value]
Object optional multiple
Returns:
push
() Object
Removes last element from array. This affects the length of the array!
Returns:
reduce
callback
[initialValue]
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
FunctionThe 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 optionalInitialValue value
Returns:
remove
index
Removes element at index.
Parameters:
-
index
IntegerIndex of element to remove.
Returns:
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
Tries to locate element by value in the array and removes it.
Parameters:
-
index
IntegerArray top locate.
Returns:
reverse
() Array
Reverses order of elements in the array in-place. Returns the array.
Returns:
shift
() Object
Removes first element of the array. This method changes the length of an array!
Returns:
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]
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.
Returns:
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]
Sorts elements of the array in ascending order, if the sortFunction provided is used for comparing elements during sort.
Parameters:
-
[sortFunction]
Function optionalFunction used for sorting. Items shall accept two values in parameters and return -1, 0 or +1 as result.
Returns:
Example:
function sortFunction (firstItem, secondItem)
{
if (firstItem.length == secondItem.length) {
// both items are same
return 0;
} else {
if (firstItem.length < 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:
unshift
value
Inserts a value at first array position.
Parameters:
-
value
ObjectObject, which should be added to the array.
Returns:
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
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