Learning object 20 of 100
Map transformation visualizer
Watch every call of a callback: the item and index it received, and what it returned. Switch between map, filter, reduce and forEach to compare them on the same data.
Loading…
What it shows
map calls your function once per item and collects each return value into a new array with the same length. forEach calls it too but throws the return values away and returns undefined. If the callback forgets to return, map fills the new array with undefined.
Guided experiment
- Run the default map. Then change the callback to
(price) => price + 5and predict the new array. - Change the callback to use a block body
{ price * 2 }without return. What does map produce? - Switch to forEach. What does it return?
Predict
What does [1, 2, 3].map((n) => { n * 2; }) return?
Related
Lessons: Lesson 31: forEach and iteration, Lesson 32: map and transformations
Projects: Project 9: Shopping Cart Price Calculator
Source: web/assets/js/objects/object-020.js in the companion repository.