I would like to compare the values of keys in a JSON object before appending to the object, and if they are the same, add the values instead of appending.
I have:
{"carset": {
"location1": [
{"car": "volvo", "count": 5},
{"car": "mazda", "count": 7},
{"car": "toyota", "count": 10},
]
}
}
and when i hit this in my for loop:
newcar = 'volvo';
newcount = 6;
cars[i] = ({car: newcar, count: newcount});
I’d like to notice that volvo is already in the json object, and just add the newcount to the existing count. Thanks!
You need to compare with all the items in the
location1array or you can restructure your JSON to make the comparison easier