JavaScript Simplified · One concept, one day, one project · Day 23 of 32
Array Methods: map()
Transforming every item in a list at once, cleanly. You convert a list of prices into a formatted price list.
By the end of today you can
- Use
mapto transform a whole array - Pass an arrow function to an array method
- See why
mapreturns a new array and leaves the original alone - Build a price converter
You often need to turn a list into another list: prices into formatted prices, names into greetings, numbers into doubled numbers. You could write a loop every time. map does it in one clear line, and it is where arrow functions earn their keep.
1. What map Does
Definition
map goes through an array, runs a function on each item, and returns a new array of the results. The original array is untouched. You give map an arrow function that says how to transform one item.
map runs (n) => n * 2 on every item and collects the results into a new array. The original numbers is left exactly as it was.2. map with Strings and Objects
The transform can be anything: build a sentence, read a property off each object, format a value.
map is perfect whenever you need "the same change, applied to every item".Everything map does, a for loop can do. But map says its intention in one line: "transform every item into this". Less code, fewer places for a bug, and a reader sees at a glance what you meant.
Today’s Mini Project
💵 Price Converter
Take a list of plain prices and produce a list of formatted price strings, like "N1,500". One map, one arrow function, a whole list converted.
toLocaleString() adds the thousands commas. map applies the formatting to every price at once, giving a new list ready to show on a page.Use map to apply a 10% discount to every price, giving a new array of discounted numbers. Expected: each price reduced by 10%, the original list unchanged.
Given [5, 10, 15], use map to get [25, 100, 225] (each number squared).
Expected: a new array of the squares, in one line.
A loop that builds a new list is so common that JavaScript gave it a name: map. From now on, "turn this list into that list" is one readable line, not ten. This is the modern way data gets reshaped.
Checkpoint
What does map return?
A new array containing the result of running your function on each item. The original array is not changed.
What do you pass to map?
A function (usually an arrow function) that takes one item and returns its transformed version. map calls it for every item.
When is map a better choice than a for loop?
When you are transforming every item into a new item. map says that intention in one line, with less room for error.
Tick Before You Move On
- ☐ I can transform an array with map
- ☐ I can pass an arrow function to map
- ☐ I know map returns a new array and leaves the original alone
- ☐ I built the Price Converter
Quick Recap
map transforms every item in an array and returns a new array. You give map an arrow function describing how to change one item. The original array is never changed. map replaces the "loop to build a new list" pattern with one clear line.
Next: Day 24. map changes every item. But often you want only some items, or just one. Tomorrow you learn filter and find.