How can an argument grow without being explicitly told to do so?

In the next, function

why is the argument "i" increment

?

function colWidths(rows) {
      return rows[0].map(function(_, i) {
        return rows.reduce(function(max, row) {
          return Math.max(max, row[i].minWidth());
        }, 0);
      });
    }

      

+3


source to share


1 answer


The function passed to the map is simply called with different i values. You can write your own version of the display function simplified

like this:



function map(arr, callback){
  let newArr = []
  for(let i = 0; i < arr.length; i++){
    newArr.push(callback(arr[i], i));
  }
  return newArr;
}

mapped = map(["Zero", "One", "Two"], function(el, i){ return i });
console.log(mapped)

      

+4


source







All Articles