有没有办法用javascript重置/清除浏览器的localStorage?
当前回答
这里的代码给出了一个不想删除的键字符串列表,然后从本地存储中的所有键中筛选这些键,然后删除其他键。
const allKeys = Object.keys(localStorage);
const toBeDeleted = allKeys.filter(value => {
return !this.doNotDeleteList.includes(value);
});
toBeDeleted.forEach(value => {
localStorage.removeItem(value);
});
其他回答
使用此选项清除localStorage:
localStorage.clear();
window.localStorage.clear(); //try this to clear all local storage
如果要从用户的本地存储中删除特定项或变量,可以使用
localStorage.removeItem("name of localStorage variable you want to remove");
这里的代码给出了一个不想删除的键字符串列表,然后从本地存储中的所有键中筛选这些键,然后删除其他键。
const allKeys = Object.keys(localStorage);
const toBeDeleted = allKeys.filter(value => {
return !this.doNotDeleteList.includes(value);
});
toBeDeleted.forEach(value => {
localStorage.removeItem(value);
});
清除sessionStorage
sessionStorage.clear();
推荐文章
- 如何将两个字符串相加,就好像它们是数字一样?
- 绑定多个事件到一个监听器(没有JQuery)?
- 在JavaScript中将JSON字符串解析为特定对象原型
- 将字符串“true”/“false”转换为布尔值
- 如何使用JavaScript代码获得浏览器宽度?
- event.preventDefault()函数在IE中无法工作
- indexOf()和search()的区别是什么?
- 错误:'types'只能在.ts文件中使用- Visual Studio Code使用@ts-check
- React-Native:应用程序未注册错误
- LoDash:从对象属性数组中获取值数组
- src和dist文件夹的作用是什么?
- jQuery UI对话框-缺少关闭图标
- 如何使用AngularJS获取url参数
- 将RGB转换为白色的RGBA
- 如何将“camelCase”转换为“Camel Case”?