This the result of my query I want to get the total of
deduction
3: {total_ot: 0, total_days: 96, total_allowance: 0, wrk_id: 3, f_name: "JOHN", l_name: "DOE",…}
daily_rate: 560
date: {2020-09-24: {work_hours: 8, adj_hour: 0}, 2020-09-25: {work_hours: 8, adj_hour: 0},…}
deduction: {1: {amount: 700}, 2: {amount: 700}}
f_name: "JOHN"
l_name: "DOE"
m_name: null
total_allowance: 0
total_days: 96
total_ot: 0
wrk_id: 3
In my computed
deducted(){ const deducted = Object.values(this.workersSummaryData) return deducted.reduce((acc, item) =>{ console.log(item.deducted) if(item.deduction) return acc + item.deduction; else return acc }, 0) } },
最佳答案
尝试使用reduce()方法并获取 amount
属性对其求和:
const sum = Object.values(obj.deduction)
.reduce((a, {amount}) => {return a + amount }, 0);
一个例子:
let obj = {
total_ot: 0, total_days: 96, total_allowance: 0, wrk_id: 3, f_name: "JOHN", l_name: "DOE",
daily_rate: 560,
date: { '2020-09-24': { work_hours: 8, adj_hour: 0 }, '2020-09-25': { work_hours: 8, adj_hour: 0 } },
deduction: { 1: { amount: 700 }, 2: { amount: 700 } },
f_name: "JOHN",
l_name: "DOE",
m_name: null,
total_allowance: 0,
total_days: 96,
total_ot: 0,
wrk_id: 3
};
const sum = Object.values(obj.deduction)
.reduce((a, { amount }) => { return a + amount }, 0);
console.log(sum);
关于javascript - vue js中的嵌套数组总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64311522/