Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda