Get Html5 Localstorage Keys

Get HTML5 localStorage keys

in ES2017 you can use:

Object.entries(localStorage)

How to retrieve all localStorage items without knowing the keys in advance?

If you modify your function to this you can list all items based on key (will list the items only):

function allStorage() {

var values = [],
keys = Object.keys(localStorage),
i = keys.length;

while ( i-- ) {
values.push( localStorage.getItem(keys[i]) );
}

return values;
}

Object.keys is a new addition to JavaScript (ECMAScript 5). It lists all own keys on an object which is faster than using a for-in loop which is the option to this.

However, this will not show the keys. For that you need to return an object instead of an array (which is rather point-less IMO as this will bring you just as far as you were before with localStorage just with a different object - but for example's sake):

function allStorage() {

var archive = {}, // Notice change here
keys = Object.keys(localStorage),
i = keys.length;

while ( i-- ) {
archive[ keys[i] ] = localStorage.getItem( keys[i] );
}

return archive;
}

If you want a compact format listing then do this instead - here each item in the array will have key=item which you later can split into pairs and so forth:

function allStorage() {

var archive = [],
keys = Object.keys(localStorage),
i = 0, key;

for (; key = keys[i]; i++) {
archive.push( key + '=' + localStorage.getItem(key));
}

return archive;
}

HTML5 localStorage getting key from value

visit Html5 Storage Doc to get more details. Use the following syntax to set and get the values in localstorage/sessionstorage
for storing values for a session

sessionStorage.getItem('key')
sessionStorage.setItem('key','value')

or store values permanently using

localStorage.getItem('key')
localStorage.setItem('key','value')

and u said u want to know the value but you want key then you can use the function

  localStorage.key(i);

or also you can loop through the available keys and get the desired key by cross checking the value

for(var i=0, len=localStorage.length; i<len; i++) {
var key = localStorage.key(i);
var value = localStorage[key];
if(value.equals(desired_value))
console.log(key + " => " + value);
}

html5 window.localStorage.getItemItem get keys that start with

You don't, get all the items and check them individually (code not tested):

var results = [];
for (i = 0; i < window.localStorage.length; i++) {
key = window.localStorage.key(i);
if (key.slice(0,2) === "QQ") {
results.push(JSON.parse(window.localStorage.getItem(key)));
}
}

If you want to do queries use something like IndexedDB.

HTML5 localStorage key order

Don't rely on the order of localStorage keys. You can iterate through all available keys on localStorage using localStorage[n], but that refers to a key (string) which can be used with localStorage[str_key] or localStorage.getItem(str_key).

In your case, you've confused the index of the localStorage key names with the name you assigned to the item. Therefore, don't use the key index, but your value key:

for (i=0;i<localStorage.length;i++) 
{
var key = localStorage.key(i);
if(key != null)
{
var value = localStorage.getItem(key);
$("#"+key).next(".dynamicList").empty();
$("#"+key).next(".dynamicList").append(value);
}
}

HTML5 LocalStorage: Checking if a key exists

Quoting from the specification:

The getItem(key) method must return the current value associated with the given key. If the given key does not exist in the list associated with the object then this method must return null.

You should actually check against null.

if (localStorage.getItem("username") === null) {
//...
}

How to return all items from local storage?

you can get all your localStorage keys by using . Object.keys()

const allKeys = Object.keys(localstorage);

then you can return the list like this

return <ul> 
{ allKeys.map(key => <li> {localstorage.getItem(key)} </li>) }
</ul>

Can an HTML5 localStorage key be any string?

The specification requires keys and values to be set and returned as a DOMString type value. DOMString is described in [DOM Level 3 Core][1] as:

A DOMString is a sequence of 16-bit units.

IDL Definition

valuetype DOMString sequence<unsigned short>;

The UTF-16 encoding was chosen because of its widespread industry
practice. Note that for both HTML and XML, the document character set
(and therefore the notation of numeric character references) is based
on UCS [ISO/IEC 10646]. A single numeric character reference in a
source document may therefore in some cases correspond to two 16-bit
units in a DOMString (a high surrogate and a low surrogate). For
issues related to string comparisons, refer to String Comparisons in
the DOM.

For Java and ECMAScript, DOMString is bound to the String type because
both languages also use UTF-16 as their encoding.

So officially, any legal UTF-16 string is legal as a key or a value. Not every UTF-16 codepoint is a legal character though so you should try to avoid certain symbols like "surrogate pairs", "byte-order marks" and "reserved characters".

Getting the key value of localStorage

UPDATED:

for(var key in localStorage){
console.log(key);
}

Or,

for (var i = 0; i < localStorage.length; i++){
console.log(localStorage.key(i));
}

, that is if you'd like to track the index count in the loop.

Looping through localStorage in HTML5 and JavaScript

You can use the key method. localStorage.key(index) returns the indexth key (the order is implementation-defined but constant until you add or remove keys).

for (var i = 0; i < localStorage.length; i++){
$('body').append(localStorage.getItem(localStorage.key(i)));
}

If the order matters, you could store a JSON-serialized array:

localStorage.setItem("words", JSON.stringify(["Lorem", "Ipsum", "Dolor"]));

The draft spec claims that any object that supports structured clone can be a value. But this doesn't seem to be supported yet.

EDIT: To load the array, add to it, then store:

var words = JSON.parse(localStorage.getItem("words"));
words.push("hello");
localStorage.setItem("words", JSON.stringify(words));


Related Topics



Leave a reply



Submit