我在本地存储中有这个保存的密钥,如何删除值为2且鞋尺寸为1的player_id?
[{"player_id":1,"shoe_size":3},{"player_id":2,"shoe_size":1},{"player_id":2,"shoe_size":2},
获得保存的值后,下一步该怎么做?
if(localStorage["player"]){
player = JSON.parse(localStorage["player"]);
}
答案 0 :(得分:1)
您可以使用.filter()
filter()方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素。
player = player .filter(function( obj ) {
return !(obj.player_id == 2 && obj.shoe_size == 1);
});
如果你想设置它:
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))