Methods
(static) from(arrayLike) → {Array}
Creates a new, shallow-copied Array instance from an array-like or iterable object. MDN
Example
console.dir(Array.from('foo'));
// Outputs: ["f", "o", "o"]
console.dir(Array.from([1, 2, 3], function (x) { return x + x; }));
// Outputs: [2, 4, 6]
Parameters:
Name | Type | Description |
---|---|---|
arrayLike |
* | An array-like or iterable object to convert to an array. |
Returns:
- A new Array instance.
- Type
- Array
(static) isArray(value) → {boolean}
Determines whether the passed value is an Array. MDN
Example
Array.isArray([1, 2, 3]); // true
Array.isArray({ foo: 123 }); // false
Array.isArray('foobar'); // false
Array.isArray(undefined); // false
Parameters:
Name | Type | Description |
---|---|---|
value |
* | The value to be checked. |
Returns:
- true if the value is an Array; otherwise, false.
- Type
- boolean
filter(callback, thisArgopt) → {Array}
Creates a new array with all elements that pass the test implemented by the provided function. MDN
Example
var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
var result = words.filter((function (word) { return word.length > 6; }));
console.dir(result);
// expected output: Array ["exuberant", "destruction", "present"]
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callback |
Array~requestCallback | Function to execute on each value in the array, taking 3 arguments |
|
thisArg |
* |
<optional> |
Value to use as this when executing callback. |
Returns:
- A new array with the elements that pass the test
- Type
- Array
find(callback, thisArgopt) → {*}
Returns the value of the first element in the provided array that satisfies the provided testing function. From ECMAScript 2015 (6th Edition, ECMA-262). MDN
Example
var array1 = [5, 12, 8, 130, 44];
var found = array1.find(function (element) {
return element > 10;
});
console.log(found); // expected output: 12
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callback |
Array~requestCallback | Function to execute on each value in the array, taking 3 arguments |
|
thisArg |
* |
<optional> |
Value to use as this when executing callback. |
Returns:
- The value of the first element in the array that satisfies the provided testing function. Otherwise, undefined is returned.
- Type
- *
findIndex(callback, thisArgopt) → {number}
Returns the index of the first element in the array that satisfies the provided testing function. From ECMAScript 2015 (6th Edition, ECMA-262). MDN
Example
var array1 = [5, 12, 8, 130, 44];
function findFirstLargeNumber(element) { return element > 13; }
console.log(array1.findIndex(findFirstLargeNumber)); // expected output: 3
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callback |
Array~requestCallback | The function to execute on each value in the array until the function returns true, indicating that the satisfying element was found. It takes three arguments |
|
thisArg |
* |
<optional> |
Optional object to use as this when executing callback. |
Returns:
- The index of the first element in the array that passes the test. Otherwise, -1.
- Type
- number
forEach(callback, thisArgopt) → {void}
Executes a provided function once for each array element. From ECMA-262 5.1 edition. MDN
Example
var array1 = ['a', 'b', 'c'];
array1.forEach(function (element) { console.dir(element); });
// expected output: "a"
// expected output: "b"
// expected output: "c"
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callback |
Array~requestCallback | Function to execute on each element. It accepts between one and three arguments. |
|
thisArg |
* |
<optional> |
Value to use as this when executing callback. |
Returns:
- Type
- void
includes(valueToFind, fromIndexopt) → {boolean}
Determines whether an array includes a certain value among its entries. From ECMA-262 5.1 edition.MDN
Example
var pets = ['cat', 'dog', 'bat'];
console.dir(pets.includes('cat')); // Outputs: true
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
valueToFind |
* | The value to search for |
||
fromIndex |
number |
<optional> |
0
|
The position in this array at which to begin searching for valueToFind. |
Returns:
- which is true if the value valueToFind is found within the array
- Type
- boolean
indexOf(searchElement, fromIndexopt) → {number}
Returns the first index at which a given element can be found in the array. From ECMA-262 5.1 edition.MDN
Example
var array = [2, 9, 9];
array.indexOf(2); // 0
array.indexOf(7); // -1
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
searchElement |
* | Element to locate in the array. |
||
fromIndex |
number |
<optional> |
0
|
The index to start the search at. |
Returns:
- The first index of the element in the array; -1 if not found.
- Type
- number
map(callback, thisArgopt) → {Array}
Creates a new array populated with the results of calling a provided function on every element in the calling array. From ECMA-262 5.1 edition. MND
Example
var array1 = [1, 4, 9, 16];
var map1 = array1.map(function (x) { return x * 2; });
console.dir(map1); // expected output: Array [2, 8, 18, 32]
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callback |
Array~requestCallback | Function that is called for every element of the array. Each time callback executes, the returned value is added to the new array. |
|
thisArg |
* |
<optional> |
Value to use as this when executing callback. |
Returns:
- A new array with each element being the result of the callback function.
- Type
- Array
reduce(callback, initialValueopt) → {*}
Executes a reducer function (that you provide) on each element of the array, resulting in a single output value. From ECMA-262 5.1 edition. MDN
Example
var array1 = [1, 2, 3, 4];
var reducer = function (accumulator, currentValue) {
return accumulator + currentValue;
}
// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer)); // expected output: 10
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5)); // expected output: 15
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callback |
reducerCallback | The function to execute on each element in the array (except for the first, if no initialValue is supplied). |
|
initialValue |
* |
<optional> |
Value to use as this when executing callback. |
Returns:
- The single value that results from the reduction.
- Type
- *
some(callback, thisArgopt) → {boolean}
Method tests whether at least one element in the array passes the test implemented by the provided function. From ECMA-262 5.1 edition. MDN
Example
var array = [1, 2, 3, 4, 5];
var even = function (element) {
return element % 2 === 0; // checks whether an element is even
};
console.dir(array.some(even)); // Outputs: true
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callback |
Array~requestCallback | Function to execute on each value in the array, taking 3 arguments |
|
thisArg |
* |
<optional> |
Value to use as this when executing callback. |
Returns:
- true if the callback function returns a truthy value for at least one element in the array. Otherwise, false.
- Type
- boolean
Type Definitions
requestCallback(element, indexopt, arrayopt)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
element |
* | The current element being processed in the array. |
|
index |
number |
<optional> |
The index of the current element being processed in the array. |
array |
Array |
<optional> |
The array function was called upon. |