sumBy source npm

_.sumBy(array, [iteratee=_.identity])

This method is like _.sum except that it accepts iteratee which is invoked for each element in array to generate the value to be summed. The iteratee is invoked with one argument: (value).

Arguments

  1. array (Array)

    The array to iterate over.

  2. [iteratee=_.identity] (Function|Object|string)

    The function invoked per element.

Returns (number)

Returns the sum.

Example

var objects = [
  { 'n': 4 },
  { 'n': 6 }
];

_.sumBy(objects, function(o) { return o.n; });
// => 10

// using the `_.property` callback shorthand
_.sumBy(objects, 'n');
// => 10