Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove multiple sesstionStorage values using keys?

I want to remove multiple sessionStorage values using key/s, One way is I can remove it one by one like this sessionStorage.removeItem('key1') & sessionStorage.removeItem('key2') and so on. So is there a way that I can remove multiple values like this easy sessionStorage.removeItem('key1', 'key2') or sessionStorage.removeItem(['key1', 'key2'])

like image 834
Sanjay Shr Avatar asked Apr 01 '26 23:04

Sanjay Shr


2 Answers

You can only remove items one at a time (MDN link, spec link). (Of course, you can use a list of keys and any looping construct.)

You could store your items as an array under a single key (using JSON.stringify when storing, JSON.parse when loading), and then you could remove the entire array (or replace it with an empty one) in one operation.

like image 148
T.J. Crowder Avatar answered Apr 03 '26 12:04

T.J. Crowder


There is no such way, however there is something else you could do.

Instead of storing individual items you could store objects with multiple properties and then when you remove one you get rid of all its properties.

like image 35
Andrei Dragotoniu Avatar answered Apr 03 '26 12:04

Andrei Dragotoniu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!