Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
How can I add every value with the same key together in javascript?

I want to add every value with the same key togehter. The Arrays look something like this, only with different values:

{"bank":1752000,"money":215560,"black_money":0}
{"bank":1752000,"money":215560,"black_money":0}
{"bank":1752000,"money":215560,"black_money":0}
{"bank":1752000,"money":215560,"black_money":0}
{"bank":1752000,"money":215560,"black_money":0}
{"bank":1752000,"money":215560,"black_money":0}
{"bank":1752000,"money":215560,"black_money":0}

As the result in the end i need three variables. One for bank, one for money and one for black_money. Can somebody please help me, i tried a lot of ways but none of theme worked.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Initialise an object with default values, and then loop over the array, and add each object property value to the output object.

const data = [
  { bank: 1752000, money: 215560, black_money: 0 },
  { bank: 1752000, money: 215560, black_money: 0 },
  { bank: 1752000, money: 215560, black_money: 0 },
  { bank: 1752000, money: 215560, black_money: 0 },
  { bank: 1752000, money: 215560, black_money: 0 },
  { bank: 1752000, money: 215560, black_money: 0 },
  { bank: 1752000, money: 215560, black_money: 0 }
];

const out = { bank: 0, money: 0, black_money: 0 };

for (const obj of data) {
  out.bank += obj.bank;
  out.money += obj.money;
  out.black_money += obj.black_money;
}

console.log(out);
console.log(out.bank);
console.log(out.money);
console.log(out.black_money);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!