map() function in JS

 The map() method passes each element of the called array to the specified function and returns an array containing the return value of the function.

 The function passed to map() is called in the same way as the function passed to forEach(). But the function passed to map() should have a return value. Note: map() returns a new array: it does not modify the called array.

for example:

      Requirements: Quadratic for each element in the array arr. Do not modify the array arr directly, the result returns a new array 

      accomplish:

  function square(arr){  

       return arr.map(function(item)

      {  return item*item;});  

    } 

  var arr = [1, 2, 3, 4];  

  console.log(square(arr));  

      Result: [1,4,9,16]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324975751&siteId=291194637