Warm tip: This article is reproduced from serverfault.com, please click

javascript-如何删除本地存储中的特定项目?

(javascript - How delete a specific item in localstorage?)

发布于 2020-11-28 07:56:39

在此处输入图片说明

我该如何删除优惠券?我搜索了,找不到任何解决方案。

Questioner
Hemanth
Viewed
0
129 2020-11-28 17:13:03

由于“状态”键作为字符串值存储在localStorage中,因此你需要将该值获取为JSON并进行解析以获取要删除的必需属性,然后再次设置localStorage。

let stateObj = JSON.parse(localStorage.getItem("state"));
delete stateObj.coupon;
localStorage.setItem("state", JSON.stringify(stateObj ));