0

How can I sum data of my first array with data of array in it?

one

code

this is my current code which is working on first array only

calculateTotal: function(){
  var sum=0;
  for(var i=0;i<this.accounts.length;i++){
     sum += this.accounts[i].balance;
  }
  return sum;
},

What should I change in this code to get the result i want?

2
  • Please post the source object as text instead Commented Sep 7, 2018 at 4:16
  • Can you please if this.accounts is not null. Commented Sep 7, 2018 at 4:18

4 Answers 4

2

You need not only the balance property, but you also need to add every balance property in the payments array. You can do this very concisely with reduce, passing in the outer balance as the initial value of the accumulator:

const obj = {
  accounts: [{
    balance: 150000,
    payments: [{
        balance: 100000,
      },
      {
        balance: -200000,
      }
    ]
  }]
};
const total = obj.accounts.reduce((a, { balance, payments }) => (
  a + payments.reduce((accum, { balance }) => accum + balance, balance)
), 0);
console.log(total);

Or, in object method form:

const obj = {
  calculateTotal() {
    return obj.accounts.reduce((a, { balance, payments }) => (
      a + payments.reduce((accum, { balance }) => accum + balance, balance)
    ), 0)
  },
  accounts: [{
    balance: 150000,
    payments: [{
        balance: 100000,
      },
      {
        balance: -200000,
      }
    ]
  }]
};
console.log(obj.calculateTotal());

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

2 Comments

const total = obj.accounts.reduce((a, { balance, payments }) => ( a + payments.reduce((accum, { balance }) => accum + balance, balance) ), 0); how can I use this part in my code? PS: I'm using vue.js and my code is placed in methods: {...}
Just put the function in the object instead?
1

Can you try this?

 calculateTotal: function(){
      var sum=0;
      for(var i=0;i<this.accounts.length;i++){
         sum += this.accounts[i].balance;
         // iterate over payments sub array in each account and add balance into the sum
         for(var j=0;j<this.accounts[i].payments.length;j++){
             sum += this.accounts[i].payments[j].balance;
         }
      }
      return sum;
    },

1 Comment

need 10 min time before i be able to do that.
0

Use reduce for this case

  sumOfPaymentBalance = accounts.payments.reduce((sum,currentValue)=> 
                          (sum+currentValue.balance),0);
    mainBalance = accounts.reduce(sum,paymentValue=>(sum,paymentValue.balance),0);
    sumOfPaymentBalance += mainBalance;
    console.log(sumOfBalance);

2 Comments

1 what is the difference between your way and Jaydip Rakholiya way? i mean which one has better functionality? 2 and if i want to use your way what would be the code exactly? (based on my function)
There is no much difference.My suggestion is a different approach to solve it.I have used the ES6 syntax and reduce method.When you want to get a single value after performing a same operation over each array of objects then reduce is more relevant and meant for it
0

I just tried making a more generic type of solution to count a given key which can be nested into as many arrays or objects as you wish.

summarize = (obj, sum_key) => {
  var sum = 0;
  for ( key in obj ) {
    if (key == sum_key) {
      sum += obj[key];
    } else if (typeof obj[key] == 'object' && obj[key]) {
      sum += summarize(obj[key]);
    }
  }
  return sum;
};

Now you can get the required sum with:

summarize(data, 'balance');

Comments

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.