Merge objects with the same id but sum values of the objects (ok)
https://stackoverflow.com/questions/44332180/merge-objects-with-the-same-id-but-sum-values-of-the-objects
Last updated
Was this helpful?
https://stackoverflow.com/questions/44332180/merge-objects-with-the-same-id-but-sum-values-of-the-objects
Last updated
Was this helpful?
Asked 5 years, 7 months agoModified Viewed 3k times1
I want to reduce my array of objects by comparing previous and current object from the array, if the id of previous object is different then current object, then I write the previous object to my result list and override it with the current object else I sum the values of both objects. In the end it should be a reduced array, no duplicates.
I have data like this:
And want to reduce it to this form:
Here is my not correctly working solution so far:
My result looks good, it reduce all duplicates but it does not sum the values of objects with the same ids, it just overrides the ids with the last object.
Sorted by: Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 5
You can use thisArg
parameter in forEach
loop and pass a empty object to store values.
I need to understand the simplest way of doing this. I've got an array of objects:
What I'm trying to get is simple object where its key is the month from data.incomes and the value is sum of relative month values, so the final result looks like:
Can anybody explain it to me step by step, please?
Sorted by: Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 3
solved using reduce and forEach
Inside the reduce function I'm running a forEach
on the array of keys of the incomes
object/attribute. For each key which is a date I'm checking if the accumulator of the reduce function contains an attribute for each date and creates if not. After creating the attribute I'm summing the value for the current date attribute.
Maybe this is not the pretties solutions but you can do it like this, the function is of course not necessary.
Assuming that this information may be useful to readers who may be unable to obtain necessary guidance (due to various possible reasons), here is one possible way to achieve the objective (solution):
Explanation
Extract only the incomes
from the data
array
For each income
object, get the key-value pair and transform into another object of the structure {key: 20yy-mm, value: nn}
Use .flat()
to transform the result from step-2 into a 1-dimensional array
Use .reduce
to sum the value
for those cases where the key
(ie, 20yy-mm) matches.
Code-snippet
My approach here is to destructure the array. This way I have all the data of the incomes of group A in the variable A
and the same for B
.
Then I do a double loop to compare both objects data and see if the dates match. If so, sum the incomes and add the data to the total
object.
Follow16.7k44 gold badges8181 silver badges125125 bronze badgesasked Jun 2, 2017 at 15:1210933 silver badges1313 bronze badges
Run code snippetExpand snippetFollow16.7k44 gold badges8181 silver badges125125 bronze badgesanswered Jun 2, 2017 at 15:19116k1515 gold badges146146 silver badges169169 bronze badges
Thanks for you help. I actually wanted to know what my mistake was. I already found out what i did wrong, i have logic error, because i reference to my first object instead of doing a deep copy. And your code is somewhat irritating by using the keyword this. I think it is not good to use this in javascript, specially here. :) –
I think it is not good to use this in javascript
why is that? –
In your example "this" is irritating, but it works fine, but not a good design from my point. It is not clear what "this" actually is. So for me and a lot of other developer will agree that "this" can be confusing ("this" in object scope or object's function scope?) and bring inconsistent behavior. Here is how i changed your code: –
4
For a version with , you could use a hash table as reference to the same company with a closure over the hash table.
Asked 12 months agoModified Viewed 3k times2
Followasked Jan 20, 2022 at 16:5233711 gold badge33 silver badges1414 bronze badges
Run code snippetExpand snippetFollowanswered Jan 20, 2022 at 16:575,0883333 gold badges4141 silver badges5151 bronze badges
1Thanks a lot! I tried to do almost the same using Object.entries() not Object.keys and that was where I had an error coming from. –
1@cmgchess this answer can be improved by adding some useful explanation... –
1@Gass i was in a hurry. added some explanation –
1
Followanswered Jan 20, 2022 at 17:118911 silver badge33 bronze badges
Maybe it's not the prettiest on but it's working and it's also easy for me to understand so it's absolutely fine.👍 Thank you. –
@Galterius you can improve it easily by reducing the length of your variable names. They should be descriptive but not that much ;) –
1I know, sometime I have problem naming vars :)))) –
1
Run code snippetExpand snippetFollowanswered Jan 20, 2022 at 17:253,53922 gold badges77 silver badges2121 bronze badges1