• map()

    The map() method processes the elements in their original order and returns a new array with the values of the elements processed by the original array call. You can think of it as mapping the original array.

    Note: the **map()** method does not detect empty arrays.

    Array. map(function(currentValue,index,arr), thisValue) Var wallets = people.map(function (dude) {return dude.wallet; });Copy the code
  • forEach()

The forEach() method is used to iterate over each element of the array, passing the element to the callback function. Note: **forEach()** does not call the callback function for an empty array.

Array. forEach(function(currentValue, index, arr), thisValue) People.foreach (function (dude) {dude.pickupsoap (); });Copy the code
  • reduce()

The reduce() method does some sort of calculation of the front and back items in the array and adds up the final value.

Array. reduce(function(Total, currentValue, currentIndex, ARR), initialValue) Var totalMoney = wallets. Reduce (function (countedMoney, wallet) { return countedMoney + wallet.money; }, 0);Copy the code
  • filter()

The filter() method filters out the qualifying items from the array to form a new array.

Array. filter(function(currentValue,index,arr), thisValue) Var fatWallets = wallets. Filter (function (wallet) {return wallet. Money > 100; });Copy the code

conclusion

  • Similarities:
  1. Each item in the array is iterated over;
  2. The map(), forEach(), and filter() methods support three parameters forEach anonymous function executed: the current element, the index of the current element, and the array to which the current element belongs.
  3. This in anonymous functions refers to the window.
  • Difference:
  1. Map () is faster than forEach().
  2. Map () and filter() return a new array without affecting the original array; ForEach () does not generate a new array, returns undefined; The reduce() function reduces an array to a single value (such as summation or quadrature);
  3. ForEach () does not return. ForEach () does not break.
  4. Reduce () takes four arguments, the first of which is the initial value.