Html5 window.localStorage.getItemItem get keys that start with

How to use

    window.localStorage.getItemItem();

      

to point to items on localstarage that start with QQ. In my case, the key the key could be: QQ + 3 digits, so I just need to specify which starts with QQ.

+3


source to share


4 answers


You do not receive all items and check them separately (code unverified):

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 make queries, use something like IndexedDB .

+5


source


Went through this and here is the solution in es6:



let search = 'QQ';
let values = Object.keys(localStorage)
                   .filter( (key)=> key.startsWith(search) )
                   .map( (key)=> localStorage[key] );

      

+1


source


for ( var info in window.localStorage ){
 if ( info.substring( 0 , 2 ) === "QQ"){
  var data = window.localStorage.getItem( info ) );
  console.log(info);
 }

      

code not checked

0


source


I am using this:

 var keyIndex = 0;
 var thisKey = window.localStorage.key(keyIndex);

 while(thisKey != '' && thisKey != undefined)
 {
    if (thisKey.substr(0, 2) == 'QQ')
    {
       // do whatever you need to do with thisKey
    }

    keyIndex+= 1;
    thisKey = window.localStorage.key(keyIndex);
 }

      

But robertc's solution is correct too, it's a little simpler. I actually changed my code to make a for loop.

0


source







All Articles