React
Academy

Array Methods in Ecmascript 2020


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 fasdfdsfsfads sd s fdsfd
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) The value of the first element satisfying the test.
arr.findIndex(fn) The index of the first element satisfying the test.
arr.indexOf(item) Returns irst index of item (or -1 if not found).
arr.lastIndexOf(item) Like indexOf, but searching backward from the end.
arr.join(separator) Returns a new string by concatenating items.
arr.toString() Returns a new string, separated by commas.
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 at the end.

(Warning: Careful with modifications when using React state 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

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(arg1, arg2,…) Creates a new Array from a variable number of arguments.

Array methods that return iterators

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.