-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathfilter.js
More file actions
29 lines (23 loc) · 859 Bytes
/
filter.js
File metadata and controls
29 lines (23 loc) · 859 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
26
27
28
29
/**
*
filter() creates a new array with each element in the original
array if and only if the element pass the supplied predicate callback.
The predicate callback is invoked with three arguments:
- the value of the element
- the index of the element
- the Array object being traversed
The predicate callback is expected to return true if the element should
appear in the new array, otherwise false.
You can optionally specify a value to use as `this` in the callback
as the second argument to filter().
If no elements pass the predicate callback, an empty array will be returned
*/
Array.prototype.myFilter = function myFilter(callback, thisArg) {
const newArray = [];
for (let i = 0; i < this.length; i += 1) {
if (callback.call(thisArg, this[i], i, this)) {
newArray.push(this[i]);
}
}
return newArray;
};