1 | import baseIteratee from './_baseIteratee';
|
2 | import baseSum from './_baseSum';
|
3 |
|
4 | /**
|
5 | * This method is like `_.sum` except that it accepts `iteratee` which is
|
6 | * invoked for each element in `array` to generate the value to be summed.
|
7 | * The iteratee is invoked with one argument: (value).
|
8 | *
|
9 | * @static
|
10 | * @memberOf _
|
11 | * @since 4.0.0
|
12 | * @category Math
|
13 | * @param {Array} array The array to iterate over.
|
14 | * @param {Array|Function|Object|string} [iteratee=_.identity]
|
15 | * The iteratee invoked per element.
|
16 | * @returns {number} Returns the sum.
|
17 | * @example
|
18 | *
|
19 | * var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];
|
20 | *
|
21 | * _.sumBy(objects, function(o) { return o.n; });
|
22 | * // => 20
|
23 | *
|
24 | * // The `_.property` iteratee shorthand.
|
25 | * _.sumBy(objects, 'n');
|
26 | * // => 20
|
27 | */
|
28 | function sumBy(array, iteratee) {
|
29 | return (array && array.length)
|
30 | ? baseSum(array, baseIteratee(iteratee))
|
31 | : 0;
|
32 | }
|
33 |
|
34 | export default sumBy;
|