_.reduce

Applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.

  1. // Underscore/Lodash
  2. var array = [0, 1, 2, 3, 4]
  3. var result = _.reduce(array, function (previousValue, currentValue, currentIndex, array) {
  4. return previousValue + currentValue
  5. })
  6. console.log(result)
  7. // output: 10
  8.  
  9. // Native
  10. var array = [0, 1, 2, 3, 4]
  11. var result = array.reduce(function (previousValue, currentValue, currentIndex, array) {
  12. return previousValue + currentValue
  13. })
  14. console.log(result)
  15. // output: 10

Browser Support for Array.prototype.reduce()

ChromeEdgeFirefoxIEOperaSafari
3.0 ✔9.0 ✔10.5 ✔4.0 ✔