Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Total in nested array in vue js

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)
   }

 },
like image 674
Not a Pro Avatar asked Dec 09 '25 06:12

Not a Pro


1 Answers

Try to use reduce() method and get amount property to sum it:

const sum = Object.values(obj.deduction)
    .reduce((a, {amount}) => {return a + amount }, 0);

An example:

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);
like image 179
StepUp Avatar answered Dec 11 '25 19:12

StepUp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!