9078f6aea236aecce9c5c06aa716579969e0f02b
[yaffs-website] / node_modules / grunt-legacy-util / node_modules / lodash / sumBy.js
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  * @category Math
12  * @param {Array} array The array to iterate over.
13  * @param {Function|Object|string} [iteratee=_.identity] The iteratee invoked per element.
14  * @returns {number} Returns the sum.
15  * @example
16  *
17  * var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];
18  *
19  * _.sumBy(objects, function(o) { return o.n; });
20  * // => 20
21  *
22  * // The `_.property` iteratee shorthand.
23  * _.sumBy(objects, 'n');
24  * // => 20
25  */
26 function sumBy(array, iteratee) {
27   return (array && array.length)
28     ? baseSum(array, baseIteratee(iteratee))
29     : 0;
30 }
31
32 module.exports = sumBy;