javascript - remove localstorage array object base on value -
says have saved key in localstorage, how remove player_id has value of 2 , shoe size_of 1?
[{"player_id":1,"shoe_size":3},{"player_id":2,"shoe_size":1},{"player_id":2,"shoe_size":2},
what next after saved value?
if(localstorage["player"]){ player = json.parse(localstorage["player"]); }
you can use .filter()
the filter() method creates new array elements pass test implemented provided function.
player = player .filter(function( obj ) { return !(obj.player_id == 2 && obj.shoe_size == 1); });
if want 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))
Comments
Post a Comment