Essential array manipulation methods
| Command | Description |
|---|---|
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 |
| Command | Description |
|---|---|
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? |
| Command | Description |
|---|---|
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 |
| Command | Description |
|---|---|
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 |
| Command | Description |
|---|---|
arr.forEach(fn) | Iterate (no return) |
arr.join(sep) | Convert to string |
Array.from(iterable) | Create from iterable |
Array.isArray(val) | Check if array |