Reputation: 23574
I have the following json object that im iterating through:
obj = { '19': { id: '19', price: 5.55},
'20': { id: '20', price: 10.00} }
$.each(obj, function(index, value){
if(value.price < 5)
{
delete obj[index];
}
});
I just want to delete an item from the object under certain conditions. In this case, if the price is less than 5.
I've tried delete, but it doesn't do anything.
Upvotes: 5
Views: 27552
Reputation: 79850
Works fine, if the value is < 5
. In your case the value is 5.55
which is > 5
DEMO - To show the object got deleted when the value is < 5
Upvotes: 9
Reputation: 324790
It's possible that jQuery is doing something odd that you don't expect it to. Kind of like how PHP's foreach
creates a copy of the original array to work on.
Try raw JS:
obj = {...};
for( var x in obj) {
if( obj[x].price < 5) delete obj[x];
}
That said, none of your object's prices are less than 5, so obviously none of them will be deleted.
Upvotes: 0