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 Questionarrow-up-rightAsked 5 years, 7 months agoModified 5 years, 7 months agoarrow-up-rightViewed 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:

[{
    Clicks: 210,
    Company: "A",
    _id: { CompanyID: 5 }
},
{
    Clicks: 35,
    Company: "C",
    _id: { CompanyID: 3 }
},
{
    Clicks: 15,
    Company: "B",
    _id: { CompanyID: 2 }
},
{
    Clicks: 13,
    Company: "A",
    _id: { CompanyID: 5 }
}]

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.

Sharearrow-up-rightImprove this questionarrow-up-rightFollowedited Jun 2, 2017 at 15:19arrow-up-rightEmile Bergeron's user avatararrow-up-rightEmile Bergeronarrow-up-right16.7k44 gold badges8181 silver badges125125 bronze badgesasked Jun 2, 2017 at 15:12Rep's user avatararrow-up-rightReparrow-up-right10933 silver badges1313 bronze badgesAdd a commentarrow-up-right

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.

Run code snippetExpand snippetSharearrow-up-rightImprove this answerarrow-up-rightFollowedited Jun 2, 2017 at 15:24arrow-up-rightEmile Bergeron's user avatararrow-up-rightEmile Bergeronarrow-up-right16.7k44 gold badges8181 silver badges125125 bronze badgesanswered Jun 2, 2017 at 15:19Nenad Vracar's user avatararrow-up-rightNenad Vracararrow-up-right116k1515 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. :) – Reparrow-up-right Jun 2, 2017 at 21:13arrow-up-right

  • I think it is not good to use this in javascript why is that? – Nenad Vracararrow-up-right Jun 2, 2017 at 21:40arrow-up-right

  • 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: linkarrow-up-rightReparrow-up-right Jun 3, 2017 at 10:07arrow-up-right

Add a commentarrow-up-right4

For a version with Array#reducearrow-up-right, you could use a hash table as reference to the same company with a closure over the hash table.

Ask Questionarrow-up-rightAsked 12 months agoModified 3 months agoarrow-up-rightViewed 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?

Sharearrow-up-rightImprove this questionarrow-up-rightFollowasked Jan 20, 2022 at 16:52dariusz's user avatararrow-up-rightdariuszarrow-up-right33711 gold badge33 silver badges1414 bronze badgesAdd a commentarrow-up-right

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.

Run code snippetExpand snippetSharearrow-up-rightImprove this answerarrow-up-rightFollowedited Sep 30, 2022 at 15:49arrow-up-rightanswered Jan 20, 2022 at 16:57cmgchess's user avatararrow-up-rightcmgchessarrow-up-right5,0883333 gold badges4141 silver badges5151 bronze badges

Add a commentarrow-up-right1

Maybe this is not the pretties solutions but you can do it like this, the function is of course not necessary.

Sharearrow-up-rightImprove this answerarrow-up-rightFollowanswered Jan 20, 2022 at 17:11Galterius's user avatararrow-up-rightGalteriusarrow-up-right8911 silver badge33 bronze badges

Add a commentarrow-up-right1

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

  1. Extract only the incomes from the data array

  2. For each income object, get the key-value pair and transform into another object of the structure {key: 20yy-mm, value: nn}

  3. Use .flat() to transform the result from step-2 into a 1-dimensional array

  4. Use .reduce to sum the value for those cases where the key (ie, 20yy-mm) matches.

Code-snippet

Run code snippetExpand snippetSharearrow-up-rightImprove this answerarrow-up-rightFollowanswered Jan 20, 2022 at 17:25jsN00b's user avatararrow-up-rightjsN00barrow-up-right3,53922 gold badges77 silver badges2121 bronze badgesAdd a commentarrow-up-right1

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