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

99
Views
Calculating sum of properties of multiple objects

So I have an array of objects with some varying number of properties (but the property names are known), for example:

let data = [{a: 10, b: 1, c:10},
            {a: 17, b: 2, c:16},
            {a: 23, b: 3, c:41}]

I need to construct an object that sums up the values in the respective properties, so in this example I'd need to construct an object {a: 50, b: 6, c:67}

I wrote the following function to do this:

calcTotalForDataProps(data, props) {
    let summedData = {}
    for (const prop of props) {
      summedData[prop] = 0;
    }
    data.forEach((dataObj) => {
      for (const prop of props) {
        summedData[prop] += dataObj[prop];
      }
    });
    return summedData;
  }

And you call it like this:

calcTotalForDataProps(data, ['a', 'b', 'c']);

But I'm wondering if there's a much shorter way to write this with ES6?

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

0

You could map the wanted props with their new sums for getting an object as result.

function calcTotalForDataProps(data, props) {
    return data.reduce((r, o) => Object
        .fromEntries(props.map(k => [k, (r[k] || 0) + o[k]])
    ), {});
}

const data = [{ a: 10, b: 1, c: 10}, { a: 17, b: 2, c: 16 }, { a: 23, b: 3, c: 41 }]
console.log(calcTotalForDataProps(data, ['a', 'b', 'c']));

about 4 years ago · Juan Pablo Isaza Report

0

There's no need to iterate over the properties initially - you can create the property inside the other loop if it doesn't exist yet.

let data = [{a: 10, b: 1, c:10},
            {a: 17, b: 2, c:16},
            {a: 23, b: 3, c:41}]
const calcTotalForDataProps = (data, props) => {
  const summedData = {};
  for (const obj of data) {
    for (const [prop, num] of Object.entries(obj)) {
      summedData[prop] = (summedData[prop] || 0) + num;
    }
  }
  return summedData;
}

console.log(calcTotalForDataProps(data));

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!