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

318
Views
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 answers
Answer question

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 Report

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 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!