_.reduceRight

This method is like _.reduce except that it iterates over elements of collection from right to left.

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

Browser Support for Array.prototype.reduceRight()

ChromeEdgeFirefoxIEOperaSafari
3.0 ✔9.0 ✔10.5 ✔4.0 ✔