UNPKG

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