Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var arrayReduce = require('../internal/arrayReduce'),
2
baseEach = require('../internal/baseEach'),
3
createReduce = require('../internal/createReduce');
4
5
/**
6
* Reduces `collection` to a value which is the accumulated result of running
7
* each element in `collection` through `iteratee`, where each successive
8
* invocation is supplied the return value of the previous. If `accumulator`
9
* is not provided the first element of `collection` is used as the initial
10
* value. The `iteratee` is bound to `thisArg` and invoked with four arguments:
11
* (accumulator, value, index|key, collection).
12
*
13
* Many lodash methods are guarded to work as iteratees for methods like
14
* `_.reduce`, `_.reduceRight`, and `_.transform`.
15
*
16
* The guarded methods are:
17
* `assign`, `defaults`, `includes`, `merge`, `sortByAll`, and `sortByOrder`
18
*
19
* @static
20
* @memberOf _
21
* @alias foldl, inject
22
* @category Collection
23
* @param {Array|Object|string} collection The collection to iterate over.
24
* @param {Function} [iteratee=_.identity] The function invoked per iteration.
25
* @param {*} [accumulator] The initial value.
26
* @param {*} [thisArg] The `this` binding of `iteratee`.
27
* @returns {*} Returns the accumulated value.
28
* @example
29
*
30
* _.reduce([1, 2], function(total, n) {
31
* return total + n;
32
* });
33
* // => 3
34
*
35
* _.reduce({ 'a': 1, 'b': 2 }, function(result, n, key) {
36
* result[key] = n * 3;
37
* return result;
38
* }, {});
39
* // => { 'a': 3, 'b': 6 } (iteration order is not guaranteed)
40
*/
41
var reduce = createReduce(arrayReduce, baseEach);
42
43
module.exports = reduce;
44
45