Home > front end >  How do you build your own filter() that is similar to the native built in filter() with all of its p
How do you build your own filter() that is similar to the native built in filter() with all of its p

Time:07-11

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]

CodePudding user response:

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]

CodePudding user response:

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);
  • Related