Iteration
arr.forEach((v, i) => {}) | Iterate (no return value) |
arr.map(v => v * 2) | Transform each element |
arr.filter(v => v > 0) | Keep matching elements |
arr.reduce((acc, v) => acc + v, 0) | Reduce to single value |
arr.find(v => v.id === 1) | Find first match |
arr.findIndex(v => v > 5) | Find first match index |
arr.every(v => v > 0) | All match condition? |
arr.some(v => v > 0) | Any match condition? |
Mutation
arr.push(item) | Add to end |
arr.pop() | Remove from end |
arr.unshift(item) | Add to front |
arr.shift() | Remove from front |
arr.splice(i, 1, newItem) | Remove/insert at index |
arr.sort((a,b) => a-b) | Sort in-place |
arr.reverse() | Reverse in-place |
Non-mutating
arr.slice(1, 3) | Extract subarray |
[...arr1, ...arr2] | Concat (spread) |
arr.flat(depth) | Flatten nested arrays |
arr.flatMap(v => [v, v*2]) | Map + flatten |
arr.includes(val) | Check if contains value |
arr.indexOf(val) | Find index |
Array.from({length:5}, (_,i) => i) | Create array by index |