i'm trying to impletent a filter function in js. I have some problems. If i only have one argument, it works fine:
const filteredNumbers = numbers.filter((item) => item > 10)
When i try to use 2 or 3 arguments the problems start here:
const filteredNumbers = numbers.filter((item, i, arr) => arr === numbers)
How can i know, which argument is using for comparison to filter? For example:
const filteredNumbers = numbers.filter((item, index) => index > 0)
As we can see, index > 0, not item variable. How can i know in code, which value is used?
numbers.filter = function(callback, index, array) {
const result = [];
let count = 0;
for (let i = 0; i < this.length; i++) {
if (callback(this[i])) {
result[count] = this[i];
count++;
}
}
return result;
};