JavaScript Array Methods
# JavaScript Array Methods
The methods you will use constantly.
Transforming
| Method | Returns |
|---|---|
map(fn) | New array of transformed items |
filter(fn) | New array of items that pass a test |
reduce(fn, init) | A single accumulated value |
flat() | Flattened nested arrays |
flatMap(fn) | map then flat one level |
Searching
| Method | Returns |
|---|---|
find(fn) | First matching item (or undefined) |
findIndex(fn) | Index of first match (or -1) |
includes(x) | true if value exists |
indexOf(x) | Index of value (or -1) |
some(fn) / every(fn) | Boolean: any / all pass |
Adding & removing
| Method | Effect |
|---|---|
push(x) / pop() | Add / remove at end |
unshift(x) / shift() | Add / remove at start |
slice(a, b) | Copy a portion (non-mutating) |
splice(i, n) | Remove/insert in place (mutating) |
Example
js