You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.1 KiB

4 years ago
  1. var arrayReduceRight = require('./_arrayReduceRight'),
  2. baseEachRight = require('./_baseEachRight'),
  3. baseIteratee = require('./_baseIteratee'),
  4. baseReduce = require('./_baseReduce'),
  5. isArray = require('./isArray');
  6. /**
  7. * This method is like `_.reduce` except that it iterates over elements of
  8. * `collection` from right to left.
  9. *
  10. * @static
  11. * @memberOf _
  12. * @since 0.1.0
  13. * @category Collection
  14. * @param {Array|Object} collection The collection to iterate over.
  15. * @param {Function} [iteratee=_.identity] The function invoked per iteration.
  16. * @param {*} [accumulator] The initial value.
  17. * @returns {*} Returns the accumulated value.
  18. * @see _.reduce
  19. * @example
  20. *
  21. * var array = [[0, 1], [2, 3], [4, 5]];
  22. *
  23. * _.reduceRight(array, function(flattened, other) {
  24. * return flattened.concat(other);
  25. * }, []);
  26. * // => [4, 5, 2, 3, 0, 1]
  27. */
  28. function reduceRight(collection, iteratee, accumulator) {
  29. var func = isArray(collection) ? arrayReduceRight : baseReduce,
  30. initAccum = arguments.length < 3;
  31. return func(collection, baseIteratee(iteratee, 4), accumulator, initAccum, baseEachRight);
  32. }
  33. module.exports = reduceRight;