In my application I would like that in a moment all the keys of my localstorage will be deleted, with the exception of all the keys that contain the word "wizard".
Commands such as
localstorage.clear();
will erase everything, and I just want to keep those that have the word "wizard", I have tried in this way, but I get errors because if I delete a match, in the next iteration a key will be skipped, I will get the error that is trying to search a match in a position that will now be null, since it has been deleted. how can I solve that?
this is my code:
for ( var i = 0, len = localStorage.length; i < len; ++i ) {
//if the key not contain the word "wizard" will be erased
if( localStorage.getItem(localStorage.key(i)).search("wizard")==-1){
localstorage.removeItem( localStorage.getItem( localStorage.key( i ) ) );
}
}
In my application I would like that in a moment all the keys of my localstorage will be deleted, with the exception of all the keys that contain the word "wizard".
Commands such as
localstorage.clear();
will erase everything, and I just want to keep those that have the word "wizard", I have tried in this way, but I get errors because if I delete a match, in the next iteration a key will be skipped, I will get the error that is trying to search a match in a position that will now be null, since it has been deleted. how can I solve that?
this is my code:
for ( var i = 0, len = localStorage.length; i < len; ++i ) {
//if the key not contain the word "wizard" will be erased
if( localStorage.getItem(localStorage.key(i)).search("wizard")==-1){
localstorage.removeItem( localStorage.getItem( localStorage.key( i ) ) );
}
}
Share
Improve this question
edited Oct 23, 2018 at 21:18
marc_s
757k184 gold badges1.4k silver badges1.5k bronze badges
asked Sep 1, 2018 at 4:12
yavgyavg
3,12112 gold badges53 silver badges135 bronze badges
3 Answers
Reset to default 5You can iterate over the entries
of localStorage
, and delete the key if the value includes wizard
:
localStorage.foo = 'foo';
localStorage.bar = 'wizard1';
localStorage.baz = 'wizard2';
localStorage.buzz = 'buzz';
Object.entries(localStorage).forEach(([key, val]) => {
if (!val.includes('wizard')) delete localStorage[key];
});
console.log(Object.keys(localStorage));
Result: only bar
and baz
keys will remain.
(Cannot post as embedded snippet because stack snippets do not support localStorage)
https://jsfiddle/cLm3kg01/
If you want to keep key names which contain wizard
rather than values that contain wizard
, then use Object.keys
instead of Object.entries
to iterate over the keys:
localStorage.wizard1 = 'foo';
localStorage.wizard2 = 'bar';
localStorage.baz = 'baz';
localStorage.buzz = 'buzz';
Object.keys(localStorage).forEach((key) => {
if (!key.includes('wizard')) delete localStorage[key];
});
console.log(Object.keys(localStorage));
https://jsfiddle/cLm3kg01/6/
Copy all the items in the localStorage
in an object, then clear it. Then copy back the required items to the localStorage
.
localStorage.setItem("test", 123);
localStorage.setItem("wizard", 123);
var keyToSave = "wizard";
function clearStorageWithoutKey(keyToSave){
var items = {...localStorage};
localStorage.clear();
localStorage.setItem(keyToSave,items[keyToSave]);
console.log(localStorage);
}
clearStorageWithoutKey(keyToSave);
Do like this:
for ( var i = 0, len = localStorage.length; i < len; ++i ) {
//if the key not contain the word "wizard" will be erased
if( localStorage.key(i).search("wizard")==-1){
localstorage.removeItem( localStorage.key( i ) );
}
}
And if you're trying to search the value of item, then your code is fine. But you'll just need to use regular expression with global flag:
.search(/wizard/g)
With removing item like this:
localstorage.removeItem( localStorage.key( i ) );
But not this:
localstorage.removeItem( localStorage.getItem( localStorage.key( i ) ) );