React
Academy

Array Methods in Ecmascript 2022


Array methods that return a new array

a a
arr.concat(arg1, arg2,…) Creates an array with new things at the end.
arr.slice(start, end) Creates a subarray.
arr.filter(fn) Creates a new array with all items that pass the test.
arr.flat(depth) Creates an array with all sub-arrays flattened to the specified depth.
Depth 1: [1, [2, 3], [4, 5, [6]]] -> [1,2,3,4,5,[6]].
arr.map(fn) Creates array with the results of fn on every item.
arr.flatMap(fn) Creates array by first doing a map(fn), then a flat(1).

Array methods that return one value

a a
arr.every(fn) Returns true if all elements in array pass the test.
arr.some(fn) Returns true if at least one item passes the test.
arr.includes(item) Determines if array includes a certain item.
arr.find(fn) Returns the value of the first element satisfying the test.
arr.findIndex(fn) Returns the index of the first element satisfying the test.
arr.indexOf(item) Returns the first index of item (or -1 if not found).
arr.lastIndexOf(item) Like indexOf, but searching backward from the end.
arr.at(pos) Returns item from nth position. If negative, starts from the end.
arr.at(-1) returns last item.
arr.join(separator) Returns a new string by concatenating items, separated by commas (or separator).
arr.toString() Returns a string representation of the array.
arr.toLocaleString
(locales, options)
Returns a new string, using toLocaleString.
arr.reduce(fn) Execute fn on each item, resulting in a single output.
arr.reduceRight(fn) Like reduce(), but starting from the end.

(Warning: Careful with modifications when using React state, reducer, or Redux store. If you must, use the following methods on a cloned state, not the state itself. Or use ImmerJS.)

Array methods that modify the array

a a
arr.push(...items) Adds items to the end, returns size of array.
arr.pop() Removes an item from the end, returns it.
arr.shift() Removes an item from the beginning, returns it.
arr.unshift(...items) Adds items to the beginning, returns size of array.
arr.splice(start,
deleteCount, …items)
Removes items in the middle, adds items, returns an array containing the deleted elements.
arr.copyWithin
(target, start, end)
Copies part of an array to another location in the same array, without changing size, returns the array.
arr.fill(value, start, end) Fills all the elements of an array from a start index to an end index with a static value, returns the array.
arr.reverse() Reverses an array in place, returns the array.
arr.sort(fn) Sorts the elements of an array in place, returns the array.

Static Array methods (helpers to create new Arrays)

a a
Array.from
(arr, mapfn)
Creates a new, shallow-copied Array instance from an array-like or iterable object. Second param is a map function.
Array.of
(a1, a2, …)
Creates a new Array from a variable number of arguments.

Array methods that return iterators

a a
arr.entries() Returns an Array Iterator containing key/value pairs.
arr.keys() Returns an Array Iterator containing keys.
arr.values() Returns an Array Iterator containing values.

Array methods that return nothing

a a
arr.forEach(fn) Executes fn once for each array element.

React Academy

To get the latest version of this document, visit the handout section of ReactAcademy.live.