3

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

 },
8
  • result of my query - could you post that in a form that looks like it's in a javascript variable? at the moment, it just looks like something that wouldn't be in javascript Commented Oct 12, 2020 at 4:04
  • Based on the deduction object, I think you'll want to return acc + item.deduction.amount Commented Oct 12, 2020 at 4:05
  • @Jekrb - more like return acc + Object.values(item.deduction).map(({amount}) => amount).reduce((a, b) => a+b) - since deduction is an object with properties that are objects with a property called amount :p Commented Oct 12, 2020 at 4:08
  • Not woriking It gives result 3200, Instead of 1400 Commented Oct 12, 2020 at 4:19
  • @Jekrb yes can you give an example? Commented Oct 12, 2020 at 5:17

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

Not working to me, I think because Im getting data like this Object.values(this.workersSummaryData.deduction) Im using this
If you could put VALID format data in the question then perhaps you could be helped, as it is, the data in the question is not valid javascript so nobody can decipher it except you @NotaPro
@NotaPro - no .. DATA ... valid, javascript DATA
@NotaPro try to write console.log(workersSummaryData.deduction) before Object.values and see what values are shown
@StepUp the value is UNDEFINED
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.