Martin
Martin

Reputation: 81

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?

Upvotes: 1

Views: 1010

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386868

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

Upvotes: 4

CertainPerformance
CertainPerformance

Reputation: 371193

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

Upvotes: 2

Related Questions