// 数组分组求和 let groupSum = { groupBy: function (array, callback) { return new Promise((resolve) => { let groups = {}; array.forEach(item => { let group = JSON.stringify(callback(item)); groups[group] = groups[group] || []; groups[group].push(item); }); let res = Object.keys(groups).map(group => { return groups[group]; }); resolve(res) }) }, getSum: function (arr,bykey) { return new Promise((resolve) => { let res = this.groupBy(arr, function (item) { return item.shopId }).then(res => { console.log(res) let resultSum = res.map(item => { let sum = item.reduce((total, curr) => { return total + curr[bykey] }, 0); return sum }) return resultSum }) resolve(res) }) } } let testArr = [ { shopId: 1, shopName: '测试', money: 0, }, { shopId: 2, shopName: '123', money: 1 }, { shopId: 1, shopName: '测试', money: 2 }, { shopId: 1, shopName: '测试', money: 3 }, { shopId: 2, shopName: '123', money: 4 }, { shopId: 1, shopName: '测试', money: 5 }, { shopId: 2, shopName: '123', money: 6 }, ] groupSum.getSum(testArr,'money').then(res => { console.log(res) }).catch(err => { console.log(err) })