Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed JavaScript Array Iteration Methods!
You have completed JavaScript Array Iteration Methods!
Preview
Use the map method to transform elements in an array.
Snippets from the Video
const strings = ['1','2','3','4','5'];
const fruits = ['apple', 'pear', 'cherry'];
let capitalizedFruits = [];
fruits.forEach(fruit => {
let capitalizedFruit = fruit.toUpperCase();
capitalizedFruits.push(capitalizedFruit);
});
console.log(capitalizedFruits);
const prices = [5, 4.23, 6.4, 8.09, 3.20];
// Result: [ '$5.00', '$4.23', '$6.40', '$8.09', '$3.20' ]
MDN Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Like filter, map returns a new array
leaving the original array unchanged.
0:00
However unlike filter it returns a new
array with the same number of array
0:01
elements.
0:05
Say for example that you have an array
of words you want to pluralize.
0:06
Or you could use map to go
through an array of temperatures,
0:11
changing them from Celsius to Fahrenheit.
0:14
Perhaps you have an array
of prices as numbers and
0:16
you want to turn them into strings and
put a dollar sign on the front of them.
0:19
Map can do this for you too.
0:23
To transform an array's items with map,
0:26
you use a callback function
to return the data you want.
0:29
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up