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 ) ) );
}
}