0

I have an object:

cont obj = [
{id: 1 , value: 4, trash: 'klfjsk'}
{id: 2 , value: 0, trash: 'klf'}
{id: 1 , value: 2, trash: 'jjjsk'}
{id: 2 , value: 7, trash: 'jsk'}
{id: 1 , value: 6, trash: 'aaaak'}
]

What I need as a result is a map wich the K is the id and the V are the sum of their values.

[{1:12}, {2:7}]

My approach is to do a for loop through the object adding each value that corresponds to the first key and then the same for the secon key. The totals would be stores in an array. Then I would make a map and set the obj keys with the array values.

Franco Hauva
  • 51
  • 1
  • 6
  • `in` iterates over properties, not values. Iterate over the values instead. Also, your `obj` is actually an array - might want to rename it to `arr` or something like that to make things a bit more clear at a glance. – CertainPerformance Aug 21 '22 at 15:31
  • I just edited the question, I didn't explain myself clearly. I'm looking a way to make it more efficient, since I have to use a nested loop to get the total value of each key. – Franco Hauva Aug 21 '22 at 15:40

0 Answers0