gpt4 book ai didi

javascript - storage.removeItem - 我如何使用它从值而不是键中删除多个项目

转载 作者:行者123 更新时间:2023-12-03 05:17:34 25 4
gpt4 key购买 nike

我希望能够从本地存储中删除值而不是键,有什么线索吗?

$(".delete").on("click", function() {
localStorage.removeItem('Property');
});

最佳答案

尝试使用setItem()并将值设置为 nullblank,例如,

$(".delete").on("click", function() {
localStorage.setItem('Property',"");
});

更新按照@qutz评论,如果您有多个项目,那么首先您需要以 JSON 格式存储值,然后您将能够解析和拼接以删除单个项目,例如,

$(".delete").on("click", function() {
var ar = JSON.parse(localStorage.getItem("Property")),
item='color';// let we wants to remove color property from json array
var index = ar.indexOf(item);
if (index > -1) { // if color property found
ar.splice(index, 1);// then remove it
}
// again set the property without having color
localStorage.setItem("Property", JSON.stringify(ar));
});

希望这会对您有所帮助,根据您使用的逻辑和方法,可能需要进行一些更改。

关于javascript - storage.removeItem - 我如何使用它从值而不是键中删除多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41541850/

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