Javascript array map method callback parameters

Javascript describes the syntax of a map method as:

arr.map (callback [, thisArg])

Parameters

  • callback is a function that creates an element of a new array using three arguments:
  • currentValue - the current element is processed in the array.
  • index - the index of the current element being processed in the array. Array
  • An array map is provided.
  • thisArg is the value to use when executing the callback.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

I can write a JS function that will capitalize the first letter of each word in an array of strings as follows:

capyyIt(['john', 'nelson mandela', 'james booth'])     // returns ['John', 'Nelson mandela', 'James booth']

function capyyIt(names) {
  return names.map(function capIt(curr) { return curr[0].toUpperCase() + curr.slice(1).toLowerCase(); });
}
  • capIt 4 ( )? , 4 , , , .
  • capIt , - - :

    function capyyIt(names) {
        return names.map(capIt);
    }
    function capIt(curr,index,arr) {
        return curr[0].toUpperCase() + curr.slice(1).toLowerCase();
    }
    
+1
1

, , , :

function capyyIt(names) {
  names.map(function(value, index, collection){
     capIt(val);
  })
}   

function capIt(name) {
  return name.toUpperCase() + curr.slice(1).toLowerCase();
}

map , , , . , - , .

+2

Source: https://habr.com/ru/post/1625338/


All Articles