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
Ask QuestionAsked 5 years, 7 months agoModified 5 years, 7 months agoViewed 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.
2 Answers
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.
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. :) – Rep Jun 2, 2017 at 21:13
I think it is not good to use this in javascript
why is that? – Nenad Vracar Jun 2, 2017 at 21:40In 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: link – Rep Jun 3, 2017 at 10:07
For a version with Array#reduce
, you could use a hash table as reference to the same company with a closure over the hash table.
Ask QuestionAsked 12 months agoModified 3 months agoViewed 3k times2
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?
4 Answers
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.
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. – dariusz Jan 20, 2022 at 17:01
1@cmgchess this answer can be improved by adding some useful explanation... – Gass Jan 21, 2022 at 6:45
1@Gass i was in a hurry. added some explanation – cmgchess Jan 21, 2022 at 13:59
Maybe this is not the pretties solutions but you can do it like this, the function is of course not necessary.
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. – dariusz Jan 20, 2022 at 17:18
@Galterius you can improve it easily by reducing the length of your variable names. They should be descriptive but not that much ;) – Gass Jan 20, 2022 at 19:20
1I know, sometime I have problem naming vars :)))) – Galterius Jan 20, 2022 at 20:33
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 thedata
arrayFor 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 arrayUse
.reduce
to sum thevalue
for those cases where thekey
(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.
Last updated