Given the following HTML session data:
hasSession 1
user user_abc
CosttoServe599a8e8421a80 25
runningTotal 100
Maintenance599a8e8421a80 5
BusinessEnhancements599a8e8421a80 25
BusinessEnhancements599a8e8421a94 25
Maintenance599a8e8421a94 10
FrontierInvestments599a8e8421a94 5
CosttoServe599a8e8421a86 0
FrontierInvestments599a8e8421a86 5
I'm attempting to delete all keys that contain:
599a8e8421a94
Is there any way to do a like/wild card type delete on session storage?
Loop through the sessionStorage
object and check if a key contains 599a8e8421a94
, if it does, remove it:
for (var key in sessionStorage) {
if (key.indexOf('599a8e8421a94') !== -1)
sessionStorage.removeItem(key);
}
An example of it in action:
/** Define dummy data... **/
var obj = {
'no-clean': 'i will not be cleaned.',
'testing599a8e8421a94': 25,
'another_test599a8e8421a94': 'e,ndleknfew',
'599a8e8421a94somemoretesting': '2knmsdkgsdg'
};
/** Loop through our dummy data and add it to sessionStorage. **/
for (var key in obj) {
sessionStorage.setItem(key, obj[key]);
}
/** This is the part you will need, loop through sessionStorage object. **/
for (var key in sessionStorage) {
/** Check if the key contains the search string... **/
if (key.indexOf('599a8e8421a94') !== -1)
sessionStorage.removeItem(key)
}
console.log(sessionStorage)
JSFiddle - Open the console to see the remaining key/value in the sessionStorage
object.
Reading Material
How to check whether a string contains a substring in JavaScript?