lodash 数组指定字段求和

js yekong 1365℃
_.sumBy(array, [iteratee=_.identity])

这个方法类似_.summin 除了它接受 iteratee 来调用 array中的每一个元素,来生成其值排序的标准。 iteratee 会调用1个参数: (value) 。

参数

array (Array): 要迭代的数组。
[iteratee=_.identity] (Function): 调用每个元素的迭代函数。

返回

(number): 返回总和。

例子

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


vue实例

computed: {
    totalPrice: function () {
      var that = this;
      return _.sumBy(that.data.applyCosts, 'applyCost.totalPrice');
    }
},
喜欢 (2)