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

javascript - Remove multiple localStorage items - Stack Overflow

programmeradmin0浏览0评论

I have two 'localStorage' items, summaryForm and projectForm.

Currently I use two statements to remove them,

localStorage.removeItem("summaryForm");
localStorage.removeItem("projectForm");

Is it possible to join these two statements to one?

I have two 'localStorage' items, summaryForm and projectForm.

Currently I use two statements to remove them,

localStorage.removeItem("summaryForm");
localStorage.removeItem("projectForm");

Is it possible to join these two statements to one?

Share Improve this question asked May 25, 2018 at 16:17 Gibin EaliasGibin Ealias 2,8595 gold badges24 silver badges41 bronze badges 2
  • 2 localSrorage.clear() will clear the entire localStorage if thats what you want – Nandita Sharma Commented May 25, 2018 at 16:21
  • Thanks for the comment. Unfortunately that is not what I am looking for. – Gibin Ealias Commented May 25, 2018 at 17:15
Add a comment  | 

1 Answer 1

Reset to default 15

You could just put the keys to remove in an array and loop over them:

let keysToRemove = ["summaryForm", "projectForm"];

for (key of keysToRemove) {
    localStorage.removeItem(key);
}

Using a full loop, or, using forEach:

keysToRemove.forEach(k =>
    localStorage.removeItem(k))

Or use localStorage.clear() if you want to just clear everything.

发布评论

评论列表(0)

  1. 暂无评论