gpt4 book ai didi

javascript - 根据值删除本地存储数组对象

转载 作者:行者123 更新时间:2023-12-03 10:01:32 25 4
gpt4 key购买 nike

说我在本地存储中有这个已保存的 key ,如何删除值为 2 且鞋子 size_of 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"]);
}

最佳答案

您可以使用.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);
});

如果你想设置它:

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))

关于javascript - 根据值删除本地存储数组对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30568573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com