Change Every Item: map
At a bakery, a tray of plain biscuits goes into a machine that puts chocolate on each one. A new tray comes out: the same number of biscuits, in the same order, each one changed in the same way. map is that machine for arrays.
Changing every item with a loop
To get the prices of a menu with 10% off, you would write a loop that builds a new array:
const prices = [20, 50, 10];
const sale = [];
for (const price of prices) {
sale.push(price * 0.9);
}
console.log(sale);
[18, 45, 9]
map
map does that loop for you. You give it a function, and it calls the function on every item, in order. What the function returns becomes the item of the new array:
const prices = [20, 50, 10];
const sale = prices.map((price) => price * 0.9);
console.log(sale);
console.log(prices);
[18, 45, 9]
[20, 50, 10]
The function is the short arrow function you know. It takes one item, price, and returns price * 0.9. The new array always has as many items as the old one. And the old array stays as it was.
Any change you like
The function can turn an item into anything, a text too:
const pets = ["cat", "dog", "fish"];
console.log(pets.map((pet) => pet.toUpperCase()));
console.log(pets.map((pet) => `I have a ${pet}`));
console.log(pets.map((pet) => pet.length));
["CAT", "DOG", "FISH"]
["I have a cat", "I have a dog", "I have a fish"]
[3, 3, 4]
A map often comes right before a join: first change every item, then make one line of them.
Break it on purpose
Write the function with curly brackets, and forget return:
const loud = pets.map((pet) => {
pet.toUpperCase();
});
console.log(loud);
[undefined, undefined, undefined]
The function works out each word in capitals, then throws it away: with curly brackets, a function returns nothing unless it says return. map still makes an array of three items, but each one is undefined. Write return pet.toUpperCase();, or leave out the curly brackets, like in the short arrow function.
Mission: Call signs
What you will see: under the count of who is on duty, the dashboard says:
Call signs: VEGA / ORION / LYRA / ALTAIR / NOVA / RIGEL
On the radio, the crew uses call signs: each name in capital letters, so nobody mixes them up.
- Under
#duty-count, add a paragraph with the idcall-signs. - It says
Call signs:, then each name of the duty list in capital letters, in the same order. - Between two names, there is a
/, with a space on each side. - The other lines of the duty panel keep the names as they are written.