1const array1 = [1, 4, 9, 16];
2
3// pass a function to map
4const map1 = array1.map(x => x * 2);
5
6console.log(map1);
7// expected output: Array [2, 8, 18, 32]
1const array1 = [1, 4, 9, 16];
2
3// pass a function to map
4const map1 = array1.map(x => x * 2);
5
6console.log(map1);
7// expected output: Array [2, 8, 18, 32]
8
1let new_array = arr.map(function callback( currentValue[, index[, array]]) {
2 // return element for new_array
3}[, thisArg])
4
1const sweetArray = [2, 3, 4, 5, 35]
2const sweeterArray = sweetArray.map(sweetItem => {
3 return sweetItem * 2
4})
5
6console.log(sweeterArray)
1let numbers = [1, 2, 3, 4]
2let filteredNumbers = numbers.map(function(_, index) {
3 if (index < 3) {
4 return num
5 }
6})
7// index goes from 0, so the filterNumbers are 1,2,3 and undefined.
8// filteredNumbers is [1, 2, 3, undefined]
9// numbers is still [1, 2, 3, 4]
10
11
1const myArray = ['Sam', 'Alice', 'Nick', 'Matt'];
2
3// Appends text to each element of the array
4const newArray = myArray.map(name => {
5 return 'My name is ' + name;
6});
7console.log(newArray); // ['My name is Sam', 'My Name is Alice', ...]
8
9// Appends the index of each element with it's value
10const anotherArray = myArray.map((value, index) => index + ": " + value);
11console.log(anotherArray); // ['0: Sam', '1: Alice', '2: Nick', ...]
12
13// Starting array is unchanged
14console.log(myArray); // ['Sam', 'Alice', 'Nick', 'Matt']