-
Notifications
You must be signed in to change notification settings - Fork 1
/
15 map, filter and reduce .js
122 lines (104 loc) · 2.66 KB
/
15 map, filter and reduce .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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//Map function //basically use to transform an array
const arr = [5, 1, 8, 7, 4];
//Double- [10,2,16,14,8]
function double(x) {
return x * 2;
}
//Triple the array
function triple(x) {
return x * 3
}
//Binary values of the array
function binary(x) {
return x.toString(2);
}
const output = arr.map(double);
const output2 = arr.map(triple);
const output3 = arr.map(binary);
console.log(output);
console.log(output2);
console.log(output3);
//Filter is use to filter the value in arrays according to some specific logic
//Filter odd values from array
function isOdd(x) {
return x % 2;
}
function isEven(x) {
return x % 2 === 0;
}
function greaterThanFour(x) {
return x > 4;
}
const odd = arr.filter(isOdd);
const even = arr.filter(isEven);
const greater = arr.filter(greaterThanFour);
console.log(odd);
console.log(even);
console.log(greater);
//Reduce - As the name reduce it actually does not reduce anything
//Sum or max
// Non reduce method
function sumOfArray(x) {
let sum = 0;
for (let i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
return sum;
}
console.log(sumOfArray(arr))
//By using reduce
const output4 = arr.reduce(function (acc, curr) { //accumulator and current
acc = acc + curr;//acc is like sum and curr like arr[i]
return acc;
}, 0)
console.log(output4);
//Max of array using normal function
function maxOfArray(x) {
let max = 0;
for (let i = 0; i < arr.length; i++) {
if (max < arr[i]) {
max = arr[i];
}
}
return max;
}
console.log(maxOfArray(arr));
//Max of array using reduce
const output5 = arr.reduce(function (acc, curr) {
if (acc < curr) {
acc = curr;
}
return acc;
}, 0)
console.log(output5)
//Example of Map
const users = [
{ firstName: "Akshad", lastName: "Jaiswal", age: 21 },
{ firstName: "Jarad", lastName: "Higgins", age: 22 },
{ firstName: "Arijit", lastName: "Singh", age: 33 },
{ firstName: "Tupac", lastName: "Shakur", age: 25 }
]
//List of full names
const fullname = users.map((x) => x.firstName + " " + x.lastName)
console.log(fullname);
// reduce example for age
const output6 = users.reduce(function (acc, curr) {
if (acc[curr.age]) {
acc[curr.age] = ++acc[curr.age];
} else {
acc[curr.age] = 1;
}
return acc;
}, {})
console.log(output6);
//first name of user whos age is les than 30
const output7 = users.filter((x) => x.age < 30).map((x) => x.firstName)
console.log(output7)
//first name of user whos age is les than 30 using reduce only
const output8 = users.reduce(function (acc, curr) {
if (curr.age < 30) {
acc.push(curr.firstName)
}
return acc
}, [])
console.log(output8)