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

317
Vistas
Own _.map() implement in JavaScript

Trying to understand this:

const _ = {};
_.map = function(list, callback){
    var storage = [];
    for(let i=0; i<list.length; i++){
        storage.push(callback(list[i], i, list));
    }
    return storage;
}

_.map([1,2,3], function(val){return val+1;})

Why does the callback require 3 args, when we clearly need only one?

_.map = function(list, callback){
    var storage = [];
    for(let i=0; i<list.length; i++){
        storage.push(callback(list[i])); //****** This works too!
    }
    return storage;
}

_.map([1,2,3], function(val){return val+1;})

Context: Doing a course on Frontend Masters, where they implemented it like the first version.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

An Array#map method can have up to 3 parameters for its callback. mentioned here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map


map((element) => { ... })
map((element, index) => { ... })
map((element, index, array) => { ... })


since you are not using the other two params index, array they would be undefined.

run the below code for both declarations to understand better.


_.map([1,2,3], function(val,index,arr){
  console.log(val)
  console.log(index)
  console.log(arr)
})

about 4 years ago · Juan Pablo Isaza Denunciar

0

It's because if you lookup at the current definition of the Array.prototype.map the callback function can receive up to three argument as you can see bellow.

map(function callbackFn(element) { ... })
map(function callbackFn(element, index) { ... })
map(function callbackFn(element, index, array){ ... })
map(function callbackFn(element, index, array) { ... }, thisArg)

Apart from the item at the given index on the array which the map function is call, you can receive the index and the array it self.

callbackFn

Function that is called for every element of arr. Each time callbackFn executes, the returned value is added to newArray.

The callbackFn function accepts the following arguments:

  • element

     The current element being processed in the array.
    
  • index Optional

     The index of the current element being processed in the array.
    
  • array Optional

     The array map was called upon.
    
  • thisArg Optional

    Value to use as this when executing callbackFn.

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