-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path54MapMethod.js
34 lines (29 loc) · 964 Bytes
/
54MapMethod.js
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
// map method
// these also take call back function as input
// similar to that of the forEach
const numbers = [3,4,6,1,8];
// const square = function(number){
// return number*number;
// // console.log(number*number); // use return in the map funtion.
// }
// map method --> we must always be returned
// const squareNumber = numbers.map(square);
// console.log(squareNumber);
// map function make new array always.
// and we can store it new array
// using Arrow Function
const squareNumber1 = numbers.map((number,index)=>{
return number*number;
});
console.log(squareNumber1);
// real use of map()
const users =[
{firstName: "abhishek", age: 22, Gender:"male"},
{firstName: "ashutosh", age: 24, Gender:"male"},
{firstName: "varsha", age: 25, Gender:"male"},
]
const userNames = users.map((user)=>{
return user.firstName;
});
console.log(userNames);
// map function returns an array from function which is being called