Array Methods: map, filter, find
The most useful tools for working with lists of data
Arrays have built-in methods — functions for convenient work with lists. The three most important:
- map — transforms each element, returns a new array
- filter — filters elements by a condition, returns a new array
- find — finds the first element satisfying the condition
Imagine a conveyor where each part passes through a machine and comes out changed. map does the same: takes each element, applies a function, and collects the results into a new array. The original stays untouched.
map — конвеєр що трансформує кожну деталь
Уяви конвеєр де кожна деталь проходить крізь верстат і виходить зміненою. map робить те саме: бере кожен елемент, застосовує функцію і збирає результати у новий масив. Оригінал залишається незайманим.
map завжди повертає новий масив тієї ж довжини.
filter — сито для масиву
Уяви сито: кидаєш всі елементи і частина проходить крізь дірочки (відповідають умові), а частина — ні. filter залишає тільки ті елементи для яких функція повертає true.
find — the first match. filter — all matches.
find — перший підхожий. filter — всі підхожі.
Ці методи не змінюють оригінальний масив — вони завжди повертають НОВИЙ. Початкові дані залишаються незайманими.