javascript 清除具有特定前缀的 localstorage 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24551578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clear localstorage values with certain prefix
提问by Trishul
I have stored several key value pairs which contains certain encrypted login information using HTML5 localstorage variables. I have added a unique prefix to all key names say TM_loginname
. Now I want to clear all the localstorage key-value pairs whose key starts with prefix TM_.
PS: I tried sessionstorage too, but it clears only when browser is closed.
我使用 HTML5 localstorage 变量存储了几个包含某些加密登录信息的键值对。我为所有键名添加了一个唯一的前缀 say TM_loginname
。现在我想清除所有以前缀 TM_ 开头的本地存储键值对。PS:我也试过 sessiontorage,但只有在浏览器关闭时它才会清除。
回答by RonyHe
Removing element while iterating is unsafe, so create an array to hold the keys that need to be removed. Then, iterate over that array to remove them:
在迭代时删除元素是不安全的,因此创建一个数组来保存需要删除的键。然后,遍历该数组以删除它们:
var arr = []; // Array to hold the keys
// Iterate over localStorage and insert the keys that meet the condition into arr
for (var i = 0; i < localStorage.length; i++){
if (localStorage.key(i).substring(0,3) == 'TM_') {
arr.push(localStorage.key(i));
}
}
// Iterate over arr and remove the items by key
for (var i = 0; i < arr.length; i++) {
localStorage.removeItem(arr[i]);
}
回答by Diego Laciar
Using lodash. Enjoy.
使用 lodash。享受。
_.forIn(window.localStorage, (value: string, objKey: string) => {
if (true === _.startsWith(objKey, 'TM_')) {
window.localStorage.removeItem(objKey);
}
});
回答by Omu
save to localStorage using
key = pref + version
保存到 localStorage 使用
key = pref + version
function remLSPref(pref, newName) {
for (var key in localStorage) {
if (key.indexOf(pref) == 0) {
if (key != newName) {
localStorage.removeItem(key);
}
}
}
}
and use like this:
并像这样使用:
var pref = 'myid_';
var key = pref + ver;
// rem old ls
remLSPref(pref, key);
回答by tbenst
A more modern (ES2017) solution is as follows:
更现代的(ES2017)解决方案如下:
Object.entries(localStorage).map(
x => x[0] # get keys
).filter(
x => x.substring(0,3)=="TM_"
).map(
x => localStorage.removeItem(x))
回答by Nit
You can either:
您可以:
- keep an array of all the items you've created yourself or
- loop over all localStorage itemsand filter to get the ones you need.
- 保留您自己创建的所有项目的数组或
- 循环遍历所有 localStorage 项目并过滤以获取您需要的项目。
回答by Nuno Ribeiro
Adapting the tip of github:
改编github的提示:
findLocalItems(query) {
var i,
results = [];
for (i in localStorage) {
if (localStorage.hasOwnProperty(i)) {
if (i.match(query) || (!query && typeof i === 'string')) {
var value = JSON.parse(localStorage.getItem(i));
results.push({ key: i, val: value });
}
}
}
return results; }
And after, calling the function:
之后,调用函数:
var keysFounded = findLocalItems(key);
if (keysFounded && keysFounded.length > 0) {
keysFounded.forEach(k => {
localStorage.removeItem(k.key);
});
}