🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## sumBy + [link](./sumBy "Link to this entry.") + [source](https://github.com/lodash/lodash/blob/4.5.0正式版/lodash.src.js#L14115 "View in source.") + [npm](https://www.npmjs.com/package/lodash.sumby "See the npm package.") ``` _.sumBy(array, [iteratee=_.identity]) ``` 这个方法类似 `_.sum`。 除了它接受 iteratee 调用每一个元素,根据返回的 value 决定如何计算。 iteratee 会传入1个参数:(value)。 ### 参数 1. array (Array) 要遍历的数组 2. [iteratee=_.identity] (Function|Object|string) 这个函数会处理每一个元素 ### 返回值 (number) 返回总和 ### 示例 ``` var objects = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }]; _.sumBy(objects, function(o) { return o.n; }); // => 20 // 使用了 `_.property` 的回调结果 _.sumBy(objects, 'n'); // => 20 ```