0
Hello, how to check which items have value equal to 0 (stock 0), json and loaded by ajax.
$.each(JSON.parse(response), function (i, deliveryTypes) {
for (var i in deliveryTypes) {
if(deliveryTypes[i].stockLevel != 0) {
alert('diferente 0');
} else {
alert('igual 0');
}
}
});
goal: if the value of the item (key) "stockLevel" is equal to 0 display a message alerting the user.
json:
{
"deliveryTypes": [
{
"name": "5326",
"estimatedDays": "7",
"price": "R$ 1,09",
"stockLevel": ""
},
{
"name": "3048",
"estimatedDays": "2",
"price": "R$ 11,00",
"stockLevel": "0"
},
{
"name": "MO00001000",
"estimatedDays": "2",
"price": "R$ 11,00",
"stockLevel": "23"
},
{
"name": "3045",
"estimatedDays": "0",
"price": "",
"stockLevel": ""
}
]
}
Add another conditional in if by checking if it is empty:
if(deliveryTypes[i].stockLevel != 0 || deliveryTypes[i].stockLevel == "") {
– Sam
The problem is you’re iterating
deliveryTypes
while you should iterate ondeliveryTypes.deliveryTypes
. Its variabledeliveryTypes
is an object that has a list, not a list.– Woss