Last change
on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
908 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | var baseIteratee = require('./_baseIteratee'),
|
---|
| 2 | baseSum = require('./_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 {Function} [iteratee=_.identity] The iteratee invoked per element.
|
---|
| 15 | * @returns {number} Returns the sum.
|
---|
| 16 | * @example
|
---|
| 17 | *
|
---|
| 18 | * var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];
|
---|
| 19 | *
|
---|
| 20 | * _.sumBy(objects, function(o) { return o.n; });
|
---|
| 21 | * // => 20
|
---|
| 22 | *
|
---|
| 23 | * // The `_.property` iteratee shorthand.
|
---|
| 24 | * _.sumBy(objects, 'n');
|
---|
| 25 | * // => 20
|
---|
| 26 | */
|
---|
| 27 | function sumBy(array, iteratee) {
|
---|
| 28 | return (array && array.length)
|
---|
| 29 | ? baseSum(array, baseIteratee(iteratee, 2))
|
---|
| 30 | : 0;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | module.exports = sumBy;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.