Cheat Sheets
Array Methods · Study material
Array Methods Cheat Sheet
| Code | Result |
|---|---|
[1, 2, 3].map((x) => x * 2) | [2, 4, 6] |
[1, 2, 3].filter((x) => x > 1) | [2, 3] |
[1, 2, 3].reduce((s, x) => s + x, 0) | 6 |
[].reduce((s, x) => s + x) | TypeError |
arr.find(fn) · arr.findIndex(fn) | element or undefined · index or -1 |
arr.findLast(fn) · arr.findLastIndex(fn) | search from the end |
arr.some(fn) · arr.every(fn) | any? · all? |
[].some(fn) · [].every(fn) | false · true |
[1, [2, [3]]].flat() | [1, 2, [3]] |
arr.flatMap((x) => [x, x]) | each element twice |
arr.toSorted((a, b) => a.n - b.n) | sorted copy by n |
["1", "2"].map(Number) | [1, 2] (not map(parseInt)) |
arr.forEach(fn) | undefined |