forked from munnasorder/JavaScript_Simple_Problem_Solving
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.js
More file actions
25 lines (23 loc) · 681 Bytes
/
filter.js
File metadata and controls
25 lines (23 loc) · 681 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
const myArray = [1,2,3,4,5,6,7,8,9];
// example 1
Array.prototype.myFind = function(cb) {
let result = [];
for (let i = 0, length = this.length; i < length; i++) {
if (cb(this[i], i, this)) result.push(this[i]);
};
return result;
}
const result = myArray.filter((doc, i, arr) => doc > 3);
console.log(result)
// output [ 4, 5, 6, 7, 8, 9 ]
// example 2
function myOwnFilter(arr, cb) {
let result = [];
for (let i = 0, length = arr.length; i < length; i++) {
if (cb(arr[i], i, arr)) result.push(arr[i]);
}
return result;
}
const result2 = myOwnFilter(myArray, (doc, i, arr) => doc > 5);
console.log(result2)
// output [ 6, 7, 8, 9 ]