Back to Cheatsheets
JavaScript

Array Methods

Essential array manipulation methods

Transformation (Returns New Array)

6 items
CommandDescription
arr.map(fn)
Transform each element
Example:[1,2,3].map(x => x * 2) // [2,4,6]
arr.filter(fn)
Keep matching elements
Example:[1,2,3].filter(x => x > 1) // [2,3]
arr.slice(start, end)
Extract portion
Example:[1,2,3].slice(1) // [2,3]
arr.concat(arr2)
Combine arrays
arr.flat(depth)
Flatten nested arrays
arr.flatMap(fn)
Map then flatten

Searching

6 items
CommandDescription
arr.find(fn)
First matching element
Example:[1,2,3].find(x => x > 1) // 2
arr.findIndex(fn)
Index of first match
arr.indexOf(val)
Index of value
arr.includes(val)
Check if exists (boolean)
arr.some(fn)
At least one matches?
arr.every(fn)
All match?

Reduction

2 items
CommandDescription
arr.reduce(fn, initial)
Reduce to single value
Example:[1,2,3].reduce((sum, x) => sum + x, 0) // 6
arr.reduceRight(fn)
Reduce from right

Mutation (Modifies Original)

7 items
CommandDescription
arr.push(val)
Add to end
arr.pop()
Remove from end
arr.unshift(val)
Add to start
arr.shift()
Remove from start
arr.splice(i, n, ...items)
Remove/insert at index
arr.sort(fn)
Sort in place
arr.reverse()
Reverse in place

Other

4 items
CommandDescription
arr.forEach(fn)
Iterate (no return)
arr.join(sep)
Convert to string
Array.from(iterable)
Create from iterable
Array.isArray(val)
Check if array