JavaScript Array Iterator Methods
.map()
Learning objective: By the end of this lesson, students will be able to use map() to iterate over an array, creating a new modified array.
map
PURPOSE: Create a new array from a source array, usually “transforming” its values.
The returned array is always the same length as the source array.
const nums = [1, 2, 3]
const squared = nums.map((num) => {
return num * num
});
// squared --> [1, 4, 9]
map returns a new array comprised of ‘transformed’ values - and how they have been transformed is up to the code we write in the callback function.
🎓 You Do
Given an array of instructors,
const instructors = ['Beryl', 'Hunter', 'Joe', 'Jurgen', 'Ben', 'David']
Use map to create a new array that adds the string “ is awesome” to each array element.
Sample output:
[
'Beryl is awesome',
'Hunter is awesome',
'Joe is awesome',
'Jurgen is awesome',
'Ben is awesome',
'David is awesome',
]