Before appending to a JSON object, I want to compare the values of keys and if they are the same, add the values together instead of simply appending.
Here is the current structure:
{"carset": {
"location1": [
{"car": "volvo", "count": 5},
{"car": "mazda", "count": 7},
{"car": "toyota", "count": 10},
]
}
}
During my for loop:
newcar = 'volvo';
newcount = 6;
cars[i] = ({car: newcar, count: newcount});
I am looking for a way to identify that 'volvo' already exists in the JSON object and add the new count value to the existing one. Your assistance is greatly appreciated!