countTotal
調(diào)用示例:
- 模板數(shù)組
let arr = [
{id: 0, price: 199.88},
{id: 1, price: 299.88},
{id: 2, price: 399.88}
];
console.log(countTotal(arr, 'price')); //899.64
- 計算函數(shù)
//計算對象數(shù)組中某個屬性合計
function countTotal(arr, keyName) {
let $total = 0;
$total = arr.reduce(function (total, currentValue, currentIndex, arr){
return currentValue[keyName] ? (total + currentValue[keyName]) : total;
}, 0);
return $total;
}