Array
15 methodsOrdered list of values, zero-indexed, dynamically sized.
arr.map(callback, thisArg?)Return a new array with the result of calling callback on every element.
Parameters
| Name | Type | Description |
|---|---|---|
| callback | (value, index, array) => any | Function producing the new element. |
| thisArg | any | Value to use as `this` in callback. |
Returns
Array
Example
[1, 2, 3].map(x => x * 2) // [2, 4, 6]
['a', 'b'].map((v, i) => `${i}:${v}`) // ['0:a', '1:b']arr.filter(callback, thisArg?)Return a new array with all elements for which callback returns truthy.
Parameters
| Name | Type | Description |
|---|---|---|
| callback | (value, index, array) => boolean | Predicate function. |
| thisArg | any | Value to use as `this` in callback. |
Returns
Array
Example
[1, 2, 3, 4].filter(x => x % 2 === 0) // [2, 4]
['', 'a', null].filter(Boolean) // ['a']arr.reduce(callback, initialValue?)Apply callback against an accumulator and each element to reduce to a single value.
Parameters
| Name | Type | Description |
|---|---|---|
| callback | (acc, value, index, array) => acc | Reducer function. |
| initialValue | any | Initial accumulator value. |
Returns
any
Example
[1, 2, 3, 4].reduce((a, b) => a + b, 0) // 10
['a', 'b', 'c'].reduce((acc, v) => ({...acc, [v]: true}), {}) // {a:true, b:true, c:true}arr.forEach(callback, thisArg?)Execute callback once for each element. Returns undefined.
Parameters
| Name | Type | Description |
|---|---|---|
| callback | (value, index, array) => void | Function to execute per element. |
| thisArg | any | Value to use as `this` in callback. |
Returns
undefined
Example
[1, 2, 3].forEach(x => console.log(x)) // logs 1, 2, 3arr.find(callback, thisArg?)Return the first element for which callback returns truthy, or undefined.
Parameters
| Name | Type | Description |
|---|---|---|
| callback | (value, index, array) => boolean | Predicate function. |
| thisArg | any | Value to use as `this` in callback. |
Returns
any
Example
[1, 2, 3, 4].find(x => x > 2) // 3
[{id: 1}, {id: 2}].find(o => o.id === 2) // {id: 2}arr.some(callback, thisArg?)Return true if at least one element satisfies the predicate.
Parameters
| Name | Type | Description |
|---|---|---|
| callback | (value, index, array) => boolean | Predicate function. |
| thisArg | any | Value to use as `this` in callback. |
Returns
boolean
Example
[1, 2, 3].some(x => x > 2) // true
[1, 2, 3].some(x => x > 5) // falsearr.every(callback, thisArg?)Return true if all elements satisfy the predicate.
Parameters
| Name | Type | Description |
|---|---|---|
| callback | (value, index, array) => boolean | Predicate function. |
| thisArg | any | Value to use as `this` in callback. |
Returns
boolean
Example
[2, 4, 6].every(x => x % 2 === 0) // true
[2, 4, 5].every(x => x % 2 === 0) // falsearr.includes(searchElement, fromIndex?)Return true if the array contains searchElement.
Parameters
| Name | Type | Description |
|---|---|---|
| searchElement | any | Value to find. |
| fromIndex | number | Start index (default 0). |
Returns
boolean
Example
[1, 2, 3].includes(2) // true
[1, 2, 3].includes(2, 2) // false
['a', 'b'].includes('a') // truearr.indexOf(searchElement, fromIndex?)Return the first index of searchElement, or -1 if not found.
Parameters
| Name | Type | Description |
|---|---|---|
| searchElement | any | Value to find. |
| fromIndex | number | Start index (default 0). |
Returns
number
Example
[1, 2, 1].indexOf(1) // 0
[1, 2, 1].indexOf(1, 1) // 2
[1, 2].indexOf(3) // -1arr.slice(start?, end?)Return a shallow copy of a portion of the array from start to end (end excluded).
Parameters
| Name | Type | Description |
|---|---|---|
| start | number | Start index (default 0). |
| end | number | End index (default length). |
Returns
Array
Example
[1, 2, 3, 4].slice(1, 3) // [2, 3]
[1, 2, 3, 4].slice(-2) // [3, 4]
[1, 2, 3].slice() // [1, 2, 3] (shallow copy)arr.splice(start, deleteCount?, ...items)Remove, replace or insert elements in place. Returns the removed elements.
Parameters
| Name | Type | Description |
|---|---|---|
| start | number | Start index. |
| deleteCount | number | Number of elements to remove. |
| items | any | Items to insert at start. |
Returns
Array
Example
let a = [1, 2, 3];
a.splice(1, 1) // [2], a == [1, 3]
a.splice(1, 0, 'x') // [], a == [1, 'x', 3]arr.concat(...values)Return a new array that is the concatenation of the array with values.
Parameters
| Name | Type | Description |
|---|---|---|
| values | ...any | Arrays or values to concatenate. |
Returns
Array
Example
[1, 2].concat([3, 4]) // [1, 2, 3, 4]
[1, 2].concat(3, [4]) // [1, 2, 3, 4]arr.sort(compareFn?)Sort the array in place. Without compareFn, elements are sorted as strings by UTF-16 code unit.
Parameters
| Name | Type | Description |
|---|---|---|
| compareFn | (a, b) => number | Comparator returning negative, 0 or positive. |
Returns
Array
Example
[3, 1, 2].sort() // [1, 2, 3]
[3, 1, 2].sort((a, b) => b - a) // [3, 2, 1]
[10, 1, 2].sort() // [1, 10, 2] (string sort!)arr.reverse()Reverse the array in place.
Returns
Array
Example
[1, 2, 3].reverse() // [3, 2, 1]arr.flat(depth?)Return a new array with sub-arrays concatenated up to depth (default 1).
Parameters
| Name | Type | Description |
|---|---|---|
| depth | number | Nested depth to flatten (default 1). |
Returns
Array
Example
[1, [2, [3, [4]]]].flat() // [1, 2, [3, [4]]]
[1, [2, [3, [4]]]].flat(2) // [1, 2, 3, [4]]
[1, [2, [3, [4]]]].flat(Infinity) // [1, 2, 3, 4]