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

How delete a specific item in localstorage?

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

enter image description here

How can i delete a coupon in this? I searched and couldn't find any solution.

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

Since the 'state' key is stored as string value in localStorage, you need to get the value as JSON and parse it to get the required property to be deleted and set the localStorage again.

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