Creating Arrays
const a = [1, 2, 3];
const b = Array(5).fill(0); // [0, 0, 0, 0, 0]
const c = Array.from({ length: 5 }, (_, i) => i); // [0, 1, 2, 3, 4]
const d = Array.from("hello"); // ['h', 'e', 'l', 'l', 'o']
const e = Array.of(1, 2, 3); // [1, 2, 3]
Iterating
| Method | Returns | Mutates |
|---|
forEach(fn) | undefined | No |
map(fn) | New array | No |
filter(fn) | New array | No |
reduce(fn, init) | Single value | No |
reduceRight(fn, init) | Single value | No |
flatMap(fn) | New array | No |
[1, 2, 3].map(x => x * 2); // [2, 4, 6]
[1, 2, 3, 4].filter(x => x > 2); // [3, 4]
[1, 2, 3].reduce((sum, x) => sum + x, 0); // 6
[[1, 2], [3]].flatMap(x => x); // [1, 2, 3]
Searching
| Method | Returns | Description |
|---|
find(fn) | Element or undefined | First match |
findIndex(fn) | Index or -1 | Index of first match |
findLast(fn) | Element or undefined | Last match |
findLastIndex(fn) | Index or -1 | Index of last match |
indexOf(val) | Index or -1 | First index of value |
lastIndexOf(val) | Index or -1 | Last index of value |
includes(val) | boolean | Contains value |
[1, 2, 3].find(x => x > 1); // 2
[1, 2, 3].findIndex(x => x > 1); // 1
[1, 2, 3].includes(2); // true
Testing
| Method | Returns | Description |
|---|
some(fn) | boolean | At least one matches |
every(fn) | boolean | All match |
[1, 2, 3].some(x => x > 2); // true
[1, 2, 3].every(x => x > 0); // true
Adding & Removing
| Method | Returns | Mutates | Description |
|---|
push(...items) | New length | Yes | Add to end |
pop() | Removed item | Yes | Remove from end |
unshift(...items) | New length | Yes | Add to start |
shift() | Removed item | Yes | Remove from start |
splice(i, n, ...items) | Removed items | Yes | Remove/insert at index |
concat(...arrays) | New array | No | Merge arrays |
toSpliced(i, n, ...items) | New array | No | Immutable splice |
const arr = [1, 2, 3];
arr.splice(1, 1, 10, 20); // arr is [1, 10, 20, 3]
[1, 2].concat([3, 4]); // [1, 2, 3, 4]
Sorting & Reversing
| Method | Returns | Mutates |
|---|
sort(fn?) | Sorted array | Yes |
reverse() | Reversed array | Yes |
toSorted(fn?) | New sorted array | No |
toReversed() | New reversed array | No |
[3, 1, 2].sort((a, b) => a - b); // [1, 2, 3] (ascending)
[3, 1, 2].sort((a, b) => b - a); // [3, 2, 1] (descending)
["b", "a"].sort(); // ["a", "b"] (lexicographic)
Flattening
[1, [2, [3]]].flat(); // [1, 2, [3]] (depth 1)
[1, [2, [3]]].flat(Infinity); // [1, 2, 3]
Slicing & Joining
| Method | Returns | Mutates | Description |
|---|
slice(start, end) | New array | No | Extract section |
join(sep) | String | No | Join elements |
at(index) | Element | No | Element at index (supports negative) |
with(index, val) | New array | No | Immutable set at index |
[1, 2, 3, 4].slice(1, 3); // [2, 3]
[1, 2, 3].join("-"); // "1-2-3"
[1, 2, 3].at(-1); // 3
[1, 2, 3].with(1, 99); // [1, 99, 3]
Spread & Destructuring
const copy = [...arr]; // Shallow copy
const merged = [...a, ...b]; // Merge arrays
const [first, ...rest] = arr; // Destructure
const unique = [...new Set(arr)]; // Remove duplicates
Static Methods
| Method | Description |
|---|
Array.isArray(val) | Check if value is an array |
Array.from(iterable, fn?) | Create array from iterable |
Array.of(...items) | Create array from arguments |
Common Recipes
// Sum
arr.reduce((sum, x) => sum + x, 0);
// Max / Min
Math.max(...arr);
Math.min(...arr);
// Group by (Object.groupBy)
Object.groupBy(users, u => u.role);
// Chunk array
function chunk(arr, size) {
return Array.from({ length: Math.ceil(arr.length / size) },
(_, i) => arr.slice(i * size, i * size + size));
}
// Remove duplicates
[...new Set(arr)];
// Count occurrences
arr.reduce((acc, val) => ({ ...acc, [val]: (acc[val] || 0) + 1 }), {});