1const array = [1, 2, 3, 4];
2
3
4const map = array.map((x, index) => {
5 console.log(index);
6 return x + index;
7});
8
9console.log(map);
1// Arrow function
2map((element) => { ... } )
3map((element, index) => { ... } )
4map((element, index, array) => { ... } )
5
6// Callback function
7map(callbackFn)
8map(callbackFn, thisArg)
9
10// Inline callback function
11map(function callbackFn(element) { ... })
12map(function callbackFn(element, index) { ... })
13map(function callbackFn(element, index, array){ ... })
14map(function callbackFn(element, index, array) { ... }, thisArg)
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]
1var numbers = [1, 4, 9];
2var doubles = numbers.map(function(num) {
3 return num * 2;
4});
5// doubles is now [2, 8, 18]. numbers still [1, 4, 9]
6