Reputation: 307
Says I have this saved key in my localstorage, how to remove the player_id that has value of 2 and shoe size_of 1?
[{"player_id":1,"shoe_size":3},{"player_id":2,"shoe_size":1},{"player_id":2,"shoe_size":2},
What to do next after I get the saved value?
if(localStorage["player"]){
player = JSON.parse(localStorage["player"]);
}
Upvotes: 0
Views: 104
Reputation: 133453
You can use .filter()
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
player = player .filter(function( obj ) {
return !(obj.player_id == 2 && obj.shoe_size == 1);
});
If you want to set it:
localStorage["player"] = JSON.stringify(player)
var player = [{"player_id":1,"shoe_size":3},{"player_id":2,"shoe_size":1},{"player_id":2,"shoe_size":2}];
player = player.filter(function(obj) {
return !(obj.player_id == 2 && obj.shoe_size == 1);
});
document.write(JSON.stringify(player))
Upvotes: 1