Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda