简体   繁体   English

如何按组删除HTML5的本地存储?

[英]How to delete local storage of HTML5 by group?

I am creating localstorage dynamically like below 我正在动态创建本地存储,如下所示

window.localStorage["DBname"+count]=JSON.stringify(values)

at the same time i am also creating some other local storage like 同时我也在创建其他一些本地存储,例如

window.localStorage["login_detail"]=login_string

window.localStorage["page_config"]=page_config

so i can not use window.localStorage.clear() ,because it will clear all my localstorage which i dont want, and also i can not use window. 所以我不能使用window.localStorage.clear() ,因为它会清除我不想要的所有本地存储,并且我也不能使用window。 localStorage.removeItem["DBname"+count] because i count know how many "count" will come while execution. localStorage.removeItem["DBname"+count]因为我计数知道执行时会出现多少个“计数”。

so is there any way to delete localstorage like group delete kind of? 所以有什么办法可以像组删除一样删除localstorage吗? help me out. 帮帮我。

Simple solution 简单的解决方案

var prefix = "DBname",
    continue = true, 
    on = 0;

while (continue) {

    continue = localStorage.getItem(prefix + on)?true:false;

    try { localStorage.removeItem(prefix + on);  } catch (e) { continue = false; }
    on += 1;
}

This will remove all local storage items beginning with DBname 这将删除所有以DBname开头的本地存储项

Tests 测试

Start: 开始:

  • DBname1 DBname1
  • DBblah DBblah
  • DBnameCats DBnameCats
  • DBname 2 数据库名称2
  • DBname 3 数据库名称3

End: 结束:

  • DBblah DBblah
  • DBnameCats DBnameCats

An even better solution 更好的解决方案

var keys = Object.keys(localStorage),
    prefix = "DBname";
for (var i = 0; i < keys.length; i += 1) {
    if (keys[i].indexOf(prefix) === 0) {
        localStorage.removeItem(keys[i]);
    }
}

Tests 测试

Start: 开始:

  • DBname1 DBname1
  • DBblah DBblah
  • foobar foob​​ar的
  • DBnameCats DBnameCats
  • DBname 2 数据库名称2
  • DBname 3 数据库名称3

End: 结束:

  • DBblah DBblah
  • foobar foob​​ar的

Dealing with support 处理支持

Add this one line: 添加这一行:

Object.keys||(Object.keys=function(r){var e=[];for(var n in r)r.hasOwnProperty(n)&&e.push(n);return e});

That will let you use An even better solution on practically every browser. 这样,您几乎可以在所有浏览器上使用更好的解决方案

I suppose you could iterate over all elements in the LocalStorage and find the keys you want to delete each individually. 我想您可以遍历LocalStorage所有元素,并找到要分别删除的键。

A quick example would be: 一个简单的例子是:

function clearLocalStorageByKeyMatch(word) {

    // Find items in the array where he key contains the word 'word'
    var filtered = localStorage.filter(function(item, key) {
        return key.indexOf(word) > -1;
    });

    Object.keys(filtered).forEach(function(item) {
        localStorage[item] = null;
    });

}


// clear localStorage where config word appear
// clearLocalStorageByKeyMatch('config');

You can also look at a library that handles that for you. 您也可以查看为您处理的图书馆。

Iterating though localStorage keys/items - NO 遍历localStorage键/项-否

Iterating over all the localStorage keys is not a good solution. 遍历所有localStorage密钥不是一个好的解决方案。 Since localStorage could be used by other applications as well, it would be very inefficient to iterate through all items in localStorage and then remove them. 由于localStorage也可以被其他应用程序使用,因此遍历localStorage中的所有项目然后将其删除将非常低效。

What is the best Approach 什么是最好的方法

Since you are the owner of you application, you better can maintain the counts. 由于您是应用程序的所有者,因此更好地维护计数。 On a rough level you can keep the "counts" you want to delete in future, may be the start and end position of count in the localStorage itself. 大致来说,您可以保留将来要删除的“计数”,它可能是localStorage本身中计数的开始和结束位置。

Once you know the start & end limit of "Count" - iterate and delete all the "DbName + Count" found in localStorage. 知道“ Count”的开始和结束限制后,就可以迭代并删除在localStorage中找到的所有“ DbName + Count”。

Another Workaround 另一个解决方法

You can have one single entity as JSON object for your application. 您可以将一个实体作为应用程序的JSON对象。 Think of it as you are name-spacing all the localStorage items and encapsulating all of them inside one single variable. 可以想一下,因为您要对所有localStorage项进行命名间隔并将它们封装在一个变量中。

var myApp={
"Dbname1":{},
"Dbname2":{},
"login_detail":{},
"page_config":{}
};

for saving this into localStorage: 将此保存到localStorage中:

localStorage["myApp"]=JSON.stringify(myApp);

for retrieving it back: 取回它:

var myApp=JSON.parse(localStorage["myApp"]);
//now you can delete whatever db you want from `myApp` and save it back to localStorage.

This will keep other application out of danger from your code. 这将使其他应用程序免受代码的威胁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM