1function listFruits() {
2 let fruits = ["apple", "cherry", "pear"]
3
4 fruits.map((fruit, index) => {
5 console.log(index, fruit)
6 })
7}
8
9listFruits()
10
11// https://jsfiddle.net/tmoreland/16qfpkgb/3/
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
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
1let numbers = [1, 2, 3, 4]
2let filteredNumbers = numbers.map(function(num, 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