with localstorage i have a load of unspecified items saved with dynamic names using a data namespace like so:
localStorage["myAppName.settings.whatever"] = something.whatever;
//and this:
localStorage["myAppName.data."+dynObj.name] = dynObj.data;
I want to keep the settings but not the data. However I won't ever know what all of the names inside of my data object are so I cannot clear them individually. I need to clear these each time my app is loaded but I must keep the settings so
localstorage.clear()
is not an option.
I have tried:
localstorage.removeItem("myAppName.data")
but no dice.
Anyone have any thoughts on how to clear dynamically named portions of localstorage?
You can loop through the keys in the localStorage and target them with a reg exp:
Object.keys(localStorage)
.forEach(function(key){
if (/^(myAppName.data.)/.test(key)) {
localStorage.removeItem(key);
}
});
Here's a similar question: HTML5 Localstorage & jQuery: Delete localstorage keys starting with a certain word
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With