方法一
let datalist = [
{
'skuId': 1,
'skuName': 'test1',
'number': 11
},
{
'skuId': 2,
'skuName': 'test2',
'number': 22
},
{
'skuId': 3,
'skuName': 'test3',
'number': null
},
]
let price = datalist.reduce((sum, e) => sum + Number(e.number || 0), 0)
console.log(price) // 输出33
方法二
const countNum = (arr, keyName)=>{
let $total = 0;
$total = arr.reduce(function (total, currentValue, currentIndex, arr){
return currentValue[keyName] ? (total + currentValue[keyName]) : total;
}, 0);
return $total;
}