var arr = [
{currency: 'GBP', total: 10},
{currency: 'GBP', total: 10},
{currency: 'JPY', total: 11},
{currency: 'GBP', total: 10},
{currency: 'GBP', total: 25},
{currency: 'EUR', total: 16},
{currency: 'GBP', total: 10},
{currency: 'GBP', total: 10}
];
var arr1 = [];
// 按币种分类汇总金额,参数:要分类的数组、分好类要返回的新数组
function classify(arr, arr1) {
var temp = {};
for (var j in arr) {
var key = arr[j].currency;
if (temp[key]) {
temp[key].total = temp[key].total + arr[j].total;
} else {
temp[key] = {};
temp[key].total = arr[j].total;
}
}
angular.forEach(temp, function(v, k) {
arr1.push({
currency: k,
total: v.total
});
});
return arr1;
}
classify(arr, arr1);
console.log('处理前:', arr);
console.log('处理后:', arr1);
![]()