最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Do not delete all the keys of the localstorage - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 5

You 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 ) ) );
发布评论

评论列表(0)

  1. 暂无评论