Summing Records and Consolidating Based on IDs in Array

Muirik :

I have an array of records that contain objects, each with an id and an amount. Now, in this array, some elements have the same id. What I need to do is iterate over this array, and first, sum up the values for records that have the same id, and then return just one record when that is the case - so I end up with an array with only unique ids -- each one also containing an amount property.

In other words, I want to take this array:

const records = [
  {id: 1, amount: 10},
  {id: 1, amount: 20},
  {id: 2, amount: 10},
  {id: 3, amount: 10},
  {id: 3, amount: -10}
];

... and produce this array:

const transformedRecords = [
  {id: 1, amount: 30},
  {id: 2, amount: 10},
  {id: 3, amount: 0}
];

I've thought about using a for-of loop for this, but that might bet pretty verbose, and I'm guessing there's a more succinct way to accomplish this - perhaps with reduce()? What's an elegant way to approach this, preferably using es6+ syntax?

Taki :

Use Array.reduce, for each iteration, check if you have an object with the current id in the accumulator, if you do, add the amounts, if not, push the current object to the accumulator :

const records = [
  {id: 1, amount: 10},
  {id: 1, amount: 20},
  {id: 2, amount: 10},
  {id: 3, amount: 10},
  {id: 3, amount: -10},
  {id: 4, amount: -10},
  {id: 4, amount: -10}
];

const result = records.reduce((acc, curr) => {
  const ndx = acc.findIndex(e => e.id === curr.id);

  if(ndx > -1) {
    acc[ndx].amount += curr.amount
  }
  else{
    acc.push(curr)
  }

  return acc;
}, [])

console.log(result)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=29662&siteId=1
ids