实现:
Array.prototype.Sum = function (fn) { return eval(this.map(x => Number(typeof fn == "function" ? fn(x) : x)).join("+")) || 0 }
用法示例:
var arr1 = [{name:'xx',value:1},{name:'xx',value:2},{name:'xx',value:3}] var arr2 = [1,2,3,4,5,6,7,8,9,10] arr1.Sum(x => x.value) //返回 6 arr1.Sum(x => x.name) //返回 0 arr2.sum() //返回 55
标签:函数,sum,value,js,xx,Sum,fn,name From: https://www.cnblogs.com/87duan/p/17599672.html