forked from Pushkar111/JavaScript-ZeroToHero
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_map.js
More file actions
41 lines (27 loc) · 1.27 KB
/
2_map.js
File metadata and controls
41 lines (27 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
map: Iterate an array elements and modify the array elements.
It takes a callback function with elements, index , array parameter and return a new array.
*/
// 1)
const numbers = [1, 2, 3, 4, 5];
let squareNumbers = numbers.map((num) => {
return num ** 2;
});
console.log("squareNumbers :", squareNumbers);
console.log("-------------------------------------------------------------------------------------------");
// 2)
const names = ["Asabeneh", "Mathias", "Elias", "Brook"];
var upperNames = names.map((name) => {
return name.toUpperCase();
});
console.log("upperNames :", upperNames);
console.log("-------------------------------------------------------------------------------------------");
// 3)
//Explicit return arrow function
var upperNames = names.map((name) => name.toUpperCase());
console.log("upperNames :", upperNames);
console.log("-------------------------------------------------------------------------------------------");
// 4)
let namesFirstThreeLetters = names.map((name) => name.charAt(0).toUpperCase() + name.slice(1, 3).toLowerCase());
console.log("namesFirstThreeLetters :", namesFirstThreeLetters);
console.log("-------------------------------------------------------------------------------------------");