UNPKG

1.15 kBJavaScriptView Raw
1import arrayReduceRight from './_arrayReduceRight.js';
2import baseEachRight from './_baseEachRight.js';
3import baseIteratee from './_baseIteratee.js';
4import baseReduce from './_baseReduce.js';
5import isArray from './isArray.js';
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
36export default reduceRight;