作者:xhl583337984 | 来源:互联网 | 2023-05-18 18:34
需求描述:去除JSON数组中paymode字段相同的项,并将paymoney累加。paylist:[{paymode:1,payname:现金,paymoney:20
需求描述:去除JSON数组中paymode字段相同的项,并将paymoney累加。
paylist:[{paymode:'1',payname:"现金",paymoney:"20"},
{paymode:'2',payname:"支付宝",paymoney:"50"},{paymode:'1',payname:"现金",paymoney:"40"}]
function UniquePay(paylist){
var payArr = [paylist[0]];
for(var i = 1; i var payItem = paylist[i];
var repeat = false;
for (var j = 0; j if (payItem.paymode == payArr[j].paymode) {
payArr[j].paymOney= parseFloat(payArr[j].paymoney)+parseFloat(payItem.paymoney);
repeat = true;
break;
}
}
if (!repeat) {
payArr.push(payItem);
}
}
return payArr;
}
通用的JSON数组去重
function uniqueArray(array, key){
var result = [array[0]];
for(var i = 1; i <array.length; i++){
var item = array[i];
var repeat = false;
for (var j = 0; j if (item[key] == result[j][key]) {
repeat = true;
break;
}
}
if (!repeat) {
result.push(item);
}
}
return result;
}