有没有办法用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();