I have a Javascript object that can have either none, one, or multiple keys. Then each key has a nested value.
Example
cart = {
"98eb9514-f403-4d08-b5f7-dd6a5ec013cb": {
"quantity": 1,
"FirstName": "John",
"LastName": "Lewis"
},
"92ef2918-6bc2-4f3b-b9b3-acf6ebe74b1f": {
"quantity": 1,
"FirstName": "Matthew",
"LastName": "Smith"
}
}
I need to check if a key has an exact nested value. If it does, then I need to add 1 to the quantity for that exact key with that exact nested value.
I am able to check if an exact key with that exact nested value exist by doing:
if (cart.hasOwnProperty(productId) &&
cart[productId]["quantity"] >= 1 &&
cart[productId]["FirstName"] == FirstName &&
cart[productId]["LastName"] == LastName) {
console.log('add one to qty somehow...')
}
However, this seems very inefficient and then I can't even figure out a way to add one to the quantity for just that exact key with that exact nested value.
If it is not clear, my question comes down to this: How do I check if a key with an exact nested value exist, and if it does, how do I add 1 to the quantity for that same exact key/nested value.
Been working on this for a day and half. Any help is much appreciated.