Junior #array#functional#immutability
map, filter, reduce dùng thế nào? Method nào của Array làm thay đổi mảng gốc?
⚡ Trả lời nhanh
`map` biến đổi từng phần tử, `filter` lọc, `reduce` gộp thành một giá trị; cả ba không đổi mảng gốc. Method mutating gồm `push`, `pop`, `splice`, `sort`, `reverse`, `fill`; bản không mutate là `toSorted`, `toReversed`, `toSpliced`, `with` (ES2023).
Ví dụ
main.js · sửa được
Mutating hay không
| Mutating (đổi mảng gốc) | Không mutate |
|---|---|
push, pop, shift, unshift | concat, slice, spread |
splice, sort, reverse, fill | map, filter, reduce, flat |
toSorted, toReversed, toSpliced, with |