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
1 Answer
Reset to default 15You 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.