这是我查询的结果
{1: {deduction: 625}, 2: {deduction: 500}, 3: {deduction: 2500}, grandTotal: 8460, otTotal: 50,…}
1: {deduction: 625}
2: {deduction: 500}
3: {deduction: 2500}
allowance: 0
grandTotal: 8460
otTotal: 50
我通过vue计算得出的错误
"TypeError: this.grand.reduce is not a function"
totaldeduct(){
return this.grand.reduce((acc, item)=>{
return acc+ Number(item.deduction)
},0)
}
答案 0 :(得分:0)
Reduce()是javascript中的数组函数。您要么想对对象使用forEach,要么将其重写为数组。
const data = [
{ deduction: 625 },
{ deduction: 500 },
{ deduction: 2500 },
{ grandTotal: 8460 },
{ otTotal: 50 },
];
function totaldeduct(data) {
return data.reduce((acc, item) => {
if(item.deduction)
return acc + item.deduction;
else return acc
}, 0);
}
let reduced = totaldeduct(data);
console.log(reduced);