-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmap.js
More file actions
65 lines (58 loc) · 979 Bytes
/
map.js
File metadata and controls
65 lines (58 loc) · 979 Bytes
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// map
// reduce -> Most powerfull method
// using map method you can only update the record
// can't add any record or remove any existing record
const arr = [1, 2, 3, 4, 5, 6];
const newArr = arr.map((x) => {
if (x % 2 === 0) {
return x * 2;
}
return x;
});
console.log(newArr);
const users = [
{
name: "yagnesh",
age: 33,
gender: "male",
},
{
name: "Virat",
age: 30,
gender: "male",
},
{
name: "rohit",
age: 28,
gender: "male",
},
{
name: "Alia",
age: 24,
gender: "female",
},
{
name: "dipeeka",
age: 35,
gender: "female",
},
{
name: "taimur",
age: 08,
gender: "male",
},
{
name: "amitabh",
age: 75,
gender: "male",
},
];
const newData = users.map((user) => {
if (user.gender === "male") {
return { ...user, occupation: "Cricketor" };
} else {
return { ...user, occupation: "Actor" };
}
});
console.log(users);
console.log(newData);