Array
Extends Object. Arrays are essential for storing, managing, and operating on sets of variables.
Instantiation
// initiate an empty array
var myEmptyArray = [];
var fruits = ['Apple', 'Banana', 'Mango'];
Note that in initialisation is always followed by a semicolon.
Properties |
---|
length
Number
The number of items within the Array. |
Methods |
---|
by(property, value)
Object | null
Designed for use with Arrays containing objects and returns the first object whose Parameters
Example
|
byGroup(property[, value])
Object | Array
Designed for use with Arrays containing Objects. The original Array is split into groups based on the value of the property and is returned as an Object. If the value parameter is not omitted an Array is returned containing all objects within the original Array with a property matching the value. Parameters
Example
|
concat(...valueN)
Array
Returns a new Array, containing the values of the joined arrays. Parameters
Example
|
every(callback [,thisArg])
Boolean
Returns Parameters
Example
|
filter(callback)
Array
Returns Array - a new array containing the elements passing the test callback function. If no elements pass the test, an empty array is returned.
The Parameters
Example
|
find(callback)
mixed
The Parameters
Example
|
findIndex(callback)
Number
The Parameters
Example
|
first()
mixed
Returns the first element in the array, or |
forEach(callback)
The Parameters
Example
|
includes(needle)
Boolean
Determines whether an array includes a certain value among its entries, returning true or false as appropriate.
|
indexOf(value [, fromIndex])
Number
Returns the index of the first instance of the value found in the array or -1 if not found. The optional Example
|
join(separator)
String
Returns a new String containing the Mixed.toString values of each item within the Array separated by separator. If separator is not supplied the separator will default to a comma.
|
keys()
Array
Returns an Array containing the keys for each index in the array. |
last()
mixed
Returns the last element of the Array, or |
lastIndexOf(value [, fromIndex])
Number
Returns the index of the last instance of the value found in the array or -1 if not found. The optional
|
map(callback)
Array
The |
pluck(property [, unique])
Array
The pluck() method is designed for use with Arrays containing Objects with similar properties and returns a new Array containing the values of the property supplied. Parameters
Example
The pluck method is particularly useful for obtaining values from data sets. If the optional unique argument is supplied the array of values returned will contain unique values only. |
pop()
mixed
Returns the last item in the Array and removes it from the end of the original Array.
|
push(...item)
Number
Add one or more items of any data type to the end of the Array. Returns the new length of the Array.
|
random()
mixed
Returns one random element from the Array. If you would like to randomize the Array see the
|
reduce(callback)
The
|
reduceRight(callback)
The |
reverse()
Array
Reverse the order of the Array and return the original Array.
|
shift()
mixed
Removes the first element of an array, and returns that element.
|
shuffle()
Array
Randomizes the order of the elements in the array and returns the intance of the array. |
slice(start [,end])
Array
Selects items of an Array between a start and end index and returns the new Array. The start argument specifies the item to start the selection from. Note that the starting index of Arrays is zero. If end is omitted, all elements from the start position and to the end of the array will be selected. Use negative numbers to select from the end of an array.
|
some(callback [, thisArg])
Boolean
Returns Parameters
Example
|
sort(compareFn)
Array
Sort the Array by item value, or using a comparison function. By default, the sort() method sorts the values as strings in alphabetical and ascending order.
Comparison functions should return a number:
In the example below, we'll use String.compare() method to perform a Unicode friendly comparison of firstname properties in the following Array:
|
splice(index, howMany [, ...item])
Array
The splice() method adds/removes items to/from an Array, and returns the removed item(s). The index required argument is a Number that specifies at what position to add/remove items. Use negative values to specify the position from the end of the array. The howmany argument is a required Number of items to remove from the original Array. You may follow the howmany argument with zero or more items to be inserted into the original Array at the index. |
toString()
String
Returns a new String representing the items of the Array. |
unshift(...item)
Number
Adds one or more new items to the beginning of an Array, and returns the new length of the Array. |