-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilterArray.js
73 lines (60 loc) · 1.93 KB
/
filterArray.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
// So What are truthy and falsy value??
//
// In JavaScript, the following values are considered falsy:
// false
// 0
// -0
// 0n (BigInt zero)
// '' (empty string)
// null
// undefined
// NaN
// All other values are considered truthy, including:*
// '0' (string containing a single zero)
// 'false' (string containing the word "false")
// [] (empty array)
// {} (empty object)
// function() {} (empty function)
// Approach 1: Using a for loop and Array.push
// One way to solve this problem is to iterate through the input array
// using a for loop, and for each element, call the filtering function
// with two arguments - the element itself and its index.
// If the filtering function returns a truthy value, add the element
// to a result array using the push method.
var filter = function(arr, fn) {
const result = [];
for(let i =0;i<arr.length;i++){
if(fn(arr[i],i)){
result.push(arr[i]);
}
}
return result;
}
// Approach 2: Using Array.reduce
// Another way to solve this problem is to use the Array.reduce method.
// We can use Array.reduce to accumulate the elements that satisfy the
// filtering function into a new array.
var filter = function(arr, fn) {
return arr.reduce((result, value, index) => {
if (fn(value, index)) {
result.push(value);
}
return result;
}, []);
};
// Approach 3: Using Array.forEach
// We can also solve this problem using the Array.forEach method.
// We can use Array.forEach to iterate through the input array, and
// for each element, call the filtering function with two
// arguments - the element itself and its index.
// If the filtering function returns a truthy value, add the
// element to a result array using the push method.
var filter = function(arr, fn) {
const result = [];
arr.forEach((value, index) => {
if (fn(value, index)) {
result.push(value);
}
});
return result;
};