主题
sumBy
js
_.sumBy(array, [iteratee=_.identity])此方法与 _.sum 类似,不同之处在于它接受 iteratee,调用 array 中的每个元素生成要求和的值。迭代器使用一个参数调用:(value)。
¥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).
新增于
¥Since
4.0.0
参数
¥Arguments
array(数组):要迭代的数组。¥
array(Array): The array to iterate over.[iteratee=_.identity](函数):每个元素调用迭代器。¥
[iteratee=_.identity](Function): The iteratee invoked per element.
返回
¥Returns
(数值):返回总和。
¥(number): Returns the sum.
示例
¥Example
js
var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];
_.sumBy(objects, function(o) { return o.n; });
// => 20
// The `_.property` iteratee shorthand.
_.sumBy(objects, 'n');
// => 20