-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiltermapreduce.js
65 lines (53 loc) · 1.74 KB
/
filtermapreduce.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
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
const arr=[5,1,3,2,6]
//map
const output = arr.map((x) => x*2)
console.log(output);
//filter
const output1= arr.filter((x) => x>4)
console.log(output1);
//reduce
const output2= arr.reduce((acumulator,curr) =>{
acumulator=acumulator+curr
//console.log("hi");
return acumulator
},0)
console.log(output2);
const output3 = arr.reduce((acc, current) => {
if (current > acc ) {
acc = current;
}
return acc;
}, 0);
console.log(output3)
const users = [
{ firstName: "Alok", lastName: "Raj", age: 23 },
{ firstName: "Ashish", lastName: "Kumar", age: 29 },
{ firstName: "Ankit", lastName: "Roy", age: 29 },
{ firstName: "Pranav", lastName: "Mukherjee", age: 50 },
];
// Get array of full name : ["Alok Raj", "Ashish Kumar", ...]
const fullNameArr = users.map((user) => user.firstName + " " + user.lastName);
console.log(fullNameArr); // ["Alok Raj", "Ashish Kumar", ...]
////////////
// Get the count/report of how many unique people with unique age are there
// like: {29 : 2, 75 : 1, 50 : 1}
// We should use reduce, why? we want to deduce some information from the array. Basically we want to get a single object as output
const report = users.reduce((acc, curr) => {
if(acc[curr.age]) {
acc[curr.age] = ++ acc[curr.age] ;
} else {
acc[curr.age] = 1;
}
return acc; //to every time return update object
}, {})
console.log(report) // {29 : 2, 75 : 1, 50 : 1}
////function chaining
const result =users.filter((user)=> user.age<30).map((user) => user.firstName)
console.log(result);
const result1= users.reduce((acc,curr) => {
if (curr.age <30){
acc.push(curr.firstName)
}
return acc;
},[])
console.log(result1);