The ones I found seem to be using removeItem and then deleting the key. I would like to ask if there is a more detailed method to delete a certain piece of data under a certain key.
Example
As shown in the picture, if I want to delete the a1709 data in this contrastdata, what should I write?
Since the contents stored in
localStorage
are actually strings, what you see is actually the result of the arraycontrastdata
being passed throughJSON.stringify
and then written tolocalStorage
.Since the native
localStorage
only handles the addition, deletion, modification and query of key-value pairs, so to process the itema1709
incontrastdata
, you can only array thecontrastdata
, and then delete the itema1709
Convert it to a string and replace it, the code is as follows:The simplest and crudest one
The native only provides basic API, you need to encapsulate the functions yourself
Here, if you want to delete a certain key, another idea is to setItem and replace it with a new contractdata
Extract contrastdata string str
Convert to object obj
Extract the key-value pair where
a1709
is located from the object and deleteSet new contrastdata
I think if you want to remove something, first it must have a unique identifier, then find a data set corresponding to this identifier in the data, and then delete the data set. For localstorage, I use it most The method is to push the object to an array, then stringify the array, and then store it in localstorage. When taking it out, you can objectify the string again. Then delete a certain data in the array based on the unique identifier.