-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMethod-Chain .js
76 lines (58 loc) · 1.65 KB
/
Method-Chain .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
66
67
68
69
70
71
72
73
74
75
76
const facebookProfiles = [
{
firstName: "Akash",
lastName: "Agarwal",
location: "rampur",
},
{
firstName: "Pritesh",
lastName: "Kumar",
location: "gurgaon",
},
{
firstName: "Sabiha",
lastName: "Khan",
location: "gurgaon",
},
{
firstName: "Suyash",
lastName: "Kashyap",
location: "alwar",
},
{
firstName: "Jay",
location: "gurgaon",
},
];
// return the full names of the facebook users who belong to guragon
// ================ method 1 (without chain) =============== //
function filterNonGuragonPeople(person) {
if (person.location === "gurgaon") {
return true;
} else {
return false;
}
}
const gurgaonPeople = facebookProfiles.filter(filterNonGuragonPeople);
function getFirstName(person) {
return person.firstName;
}
const gurgaonPeopleName = gurgaonPeople.map(getFirstName);
console.log(gurgaonPeopleName);
// ================ method 1 (with chain) =============== //
const gurgaonPeopleNameAfterChain = facebookProfiles
.filter(filterNonGuragonPeople)
.map(getFirstName);
console.log(gurgaonPeopleNameAfterChain);
// ========================================================================================== //
/**
* Ans : code will not run because return value of push method is a number.
* so we are doing number.filter()
* and this will not work, because filter method take array as an argument (via hidden door)
* and not the number
*
*
*
* Ans 3. use concat instead of push to return the array. now that array will become the input to
* filter function
*/