- To add/remove elements:
-
push(...items)
– adds items to the end, -
pop()
– extracts an item from the end, -
shift()
– extracts an item from the beginning, -
unshift(...items)
– adds items to the beginning. -
splice(pos, deleteCount, ...items)
– at indexpos
deletedeleteCount
elements and insertitems
. -
slice(start, end)
– creates a new array, copies elements from positionstart
tillend
(not inclusive) into it. -
concat(...items)
– returns a new array: copies all members of the current one and addsitems
to it. If any ofitems
is an array, then its elements are taken.
-
- To search among elements:
-
indexOf/lastIndexOf(item, pos)
– look foritem
starting from positionpos
, return the index or-1
if not found. -
includes(value)
– returnstrue
if the array hasvalue
, otherwisefalse
. -
find/filter(func)
– filter elements through the function, return first/all values that make it returntrue
. -
findIndex
is likefind
, but returns the index instead of a value.
-
- To iterate over elements:
-
forEach(func)
– callsfunc
for every element, does not return anything.
-
- To transform the array:
-
map(func)
– creates a new array from results of callingfunc
for every element. -
sort(func)
– sorts the array in-place, then returns it. -
reverse()
– reverses the array in-place, then returns it. -
split/join
– convert a string to array and back. -
reduce(func, initial)
– calculate a single value over the array by callingfunc
for each element and passing an intermediate result between the calls.
-
- Additionally:
-
Array.isArray(arr)
checksarr
for being an array.
-
Please note that methods
sort
, reverse
and splice
modify the array itself.
These methods are the most used ones, they cover 99% of use cases. But there are few others:
- arr.some(fn) checks the array. : The function
fn
is called on each element of the array similar tomap
. If any/all results aretrue
, returnstrue
, otherwisefalse
.
- arr.fill(value, start, end) – fills the array with repeating
value
from indexstart
toend
. - arr.copyWithin(target, start, end) – copies its elements from position
start
till positionend
into itself, at positiontarget
(overwrites existing).