Javascript - .map()
Code Life
By Pierre Marsaa
Transforming Every Element
From time to time, you need to turn a whole array into a new one.
Let's say every Codémon of your team gained a level after a big fight: you want the same list, with every level increased by 1.
With a loop, it takes several lines:
↗️
Loading editor...
↶
↷
map does exactly that in one line. It calls the callback on each element and collects the returned values into a new array:
↗️
Loading editor...
↶
↷
Loading editor...
Verify