Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
How do you build your own filter() that is similar to the native built in filter() with all of its parameters?

I am learning about filter() and what this method does. So far I understood what it does and how to use it. The part I am struggling with is trying to understand all of its optional parameters and what they do. I understand how to use all of its required parameters (callbackFunction, currentValue) but not its optional ones (index,arr,thisValue).

syntax: array.filter(callbackFunction(currentValue, index, arr), thisValue)

I would like to know the code behind the filter() method. I have made one using the required parameters (callbackFunction, currentValue), but couldn't figure out how to incorporate the index, arr, and thisValue into my code. If you can help me do that so that I can see what's going on inside the filter() method and what it's doing to the index,arr, and thisValue parameters, you will help me out a TON!

Here is my code so far:

// The global variable
const s = [23, 65, 98, 5];
 
Array.prototype.myFilter = function(callback) {
  const newArray = [];
  for (let i =0; i < this.length; i++){
    let currentElement = this[i]
    let check = callback(currentElement);
    //if true (item is odd) then it pass the filter and can be included in new array
    if (check){
      newArray.push(currentElement);
    }
  }
  return newArray;
};
 
const new_s = s.myFilter(function(item) {
  return item % 2 === 1;
  //if true (item is odd) then it pass the filter and can be included in new array
});

console.log(new_s) //[23, 65, 5]

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Just bind those to callback callback(currentElement, i, this), s.myFilter(function(item, index, arr) like this:

  // The global variable
  const s = [23, 65, 98, 5];

  Array.prototype.myFilter = function(callback) {
    const newArray = [];
    for (let i =0; i < this.length; i++){
      let currentElement = this[i]
      let check = callback(currentElement, i, this);
      //if true (item is odd) then it pass the filter and can be included in new array
      if (check){
        newArray.push(currentElement);
      }
    }
    return newArray;
  };

  const new_s = s.myFilter(function(item, index, arr) {
    console.log("index", index);
    console.log("arr", arr);
    return item % 2 === 1;
    //if true (item is odd) then it pass the filter and can be included in new array
  });

  console.log(new_s) //[23, 65, 5]
about 4 years ago · Juan Pablo Isaza Report

0

You are already passing the currentValue to the callback, but it seems you are missing the index and arr parameters.

let check = callback(currentElement, i, this);

The second parameter for filter is the this context for the callback function. Just add that to the function definition.

Array.prototype.myFilter = function(callback, thisContext) {

and call callback with the given thisContext:

let check = callback.call(thisContext, currentElement, i, this);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!