I have two objects that look like:
{
    "data" : [ 
        {
            "name" : "toyota",
            "type" : "cars",
            "totals" : {
                "invalid" : 4,
                "valid" : 14,
                "percentage" : 77.78,
                "total" : 18
            }
        }
    ],
    "report_id": "123wa31a22aba05"
}
I would like to merge those two objects into one object with the following set of rules:
Every two cars that have the same name and type in the data should be merged. This means that totals will be:
"totals": {
    "invalid": "invalidA" + "invalidB"
    "valid": "validA" + "validB"
    "percentage" : calculatePercentage("invalid","valid")
    "total": "invalid" + "valid"
}
If there is only sub-object with some name and type, it will just push it as it to the merged report.
What I thought: Copy object one to result object. Then iterate over the second object and insert the elements into the result object (merge if needed). I would use the for loop as I'm used from Java, but it doesn't feel a good js code. What is the proper way to merge those two object in JS?
Example to make it easier:
Object 1:
{
    "data" : [ 
        {
            "name" : "toyota",
            "type" : "cars",
            "totals" : {
                "invalid" : 4,
                "valid" : 14,
                "percentage" : 77.78,
                "total" : 18
            }
        }
    ],
    "report_id": "123wa31a22aba05"
}
Object 2:
{
    "data" : [ 
        {
            "name" : "toyota",
            "type" : "cars",
            "totals" : {
                "invalid" : 2,
                "valid" : 5,
                "percentage" : 71.42,
                "total" : 7
            }
        }
    ],
    "report_id": "123wa31a22aba06"
}
Result:
{
    "data" : [ 
        {
            "name" : "toyota",
            "type" : "cars",
            "totals" : {
                "invalid" : 6,
                "valid" : 19,
                "percentage" : 76.0,
                "total" : 25
            }
        }
    ]
}
 
    