Page 1 of 1

localstorage key values

PostPosted: Wed Sep 07, 2016 8:24 am
by TinTin
Is there any easy way we can obtain a list / array of keyvalues from the localstorage db?

At the moment management of the localstorage db assumes you are aware of the values to reference, if you want to delete and clear etc

Ideally I need to be able to list the keys within listbox ..

Any help would be appreciated.

Cheers

Re: localstorage key values

PostPosted: Wed Sep 07, 2016 1:58 pm
by Gaev
TinTin:

Is there any easy way we can obtain a list / array of keyvalues from the localstorage db?


Does not look like you are able to do this.

At the moment management of the localstorage db assumes you are aware of the values to reference, if you want to delete and clear etc

Given that you can access only the key/value pairs associated with your own website/page, this is not an unreasonable assumption.

Ideally I need to be able to list the keys within listbox ..

Assuming that you are the one setting these key/value pairs in the first place, there are work arounds ...

a) manage them all as a "javascript object" ... but save the entire contents of this object as "a single localstorage key/value" ... use stringify() and parse() methods to accomodate the restriction that localstorage values can only be strings.

OR

b) name your keys TinTin1, TinTin2, TinTin3 etc. ... so you can retrieve them within a loop.

OR

c) whenever you store a key:value in localstorage (e.g. UK:"London", Canada:"Ottawa", Germany:"Berlin"), make sure you store the keys in another (specially designated) key/value index pair (e.g. CountryKeys:"UK,Canada,Germany") ... then use this special key to fetch list of keys whose key/value pairs need to be obtained in the ListBox.

Re: localstorage key values

PostPosted: Thu Sep 08, 2016 1:38 am
by TinTin
Thanks GAEV, your method of storing array within each unique key is the best bet, I've implemented that and is working very well!

Cheers!