Search in the localStorage

Asked

Viewed 452 times

1

Well I need to do a search in the localStorage, and in this I need to know which start item with a certain character, for example:

Quero pegar todos as key que começão com pes_ ou seja no exemplo abaixo eu pegaria.
---- KEY ----------|------ VALUE ------
pes_918239812938   | {OBJ 1}
pes_123123121214   | {obj 2}
cel_13             | {obj 3}

No caso acima ele teria que mostras pes_918239812938, pes_123123121214 

I already managed to do the search using a foreach but passing what I want in itself, my idea is to assemble a function that if I pass to it pes_ as in the example it returns to me all and if in case I send pes_918239812938, it returned me the own.

function fnc_PesquisaBD(key){
  var output = "";
  for (key in localStorage){  
      output += key + "\n" ; 
      output += localStorage [ key ]+ "\n" ; 
      output +=  "\n" ; 
  }
  return output;
}

How should I do this ?

THE ANSWER INFORMED BY OUR FRIEND @SERGIO DOES NOT SATISFY ALL DOUBTS OF MY QUESTION. THE PROBLEM IS WHEN I HAVE TO PICK UP SEVERAL TUPLES.

var pessoasCelulas = getData("pescel_");
pessoasCelulas = pessoasCelulas[0];

console.log(pessoasCelulas);
for(var i = 0; i < pessoasCelulas.length; i++){
    if(pessoasCelulas[i].COD_IDENT_CELUL == w_codigo_celula){
        var pessoa = getData("pes_" +pessoasCelulas[i].COD_IDENT_PESSO);
            pessoa = pessoa[0];
        w_elemento = [pessoa.TXT_NOMEX_PESSO, pessoa.COD_IDENT_PESSO, pessoasCelulas[i].FLG_IDENT_PESSO, pessoa.FLG_STATU_PESSO, pessoasCelulas[i].FLG_STATU_PARTC];
        arrayConfig.push(w_elemento);
        console.log(arrayConfig);
    }

}

THIS CODE I USE TO SEARCH ALL PEOPLE OF A PARTICULAR CELL, BUT IT IS RETURNING ME THE FOLLOWING ON THE CONSOLE.

RETORNO NO CONSOLE LOG

ACCORDING TO WHAT IS IN MY LOCALSTORAGE SHOULD RETURN 3 RECORD.

LOCALSTORAGE

The code I’m using to search in localstorage is this:

function getData(chave) {
   return Object.keys(localStorage).filter(function(key) {
      return key.indexOf(chave) == 0;
   }).map(function(key) {
      return JSON.parse(localStorage[key]);
 });
}

TO "SOLVE" THE ASCIMA PROBLEM I NEEDED TO MAKE A LOT OF MANEUVER DUE TO THE CODE IS RETURNING ARRAY INSIDE ARRAY:

    var pessoasCelulas = getData("pescel_");
for(var i = 0; i < pessoasCelulas.length; i++){
    if(pessoasCelulas[i][0].COD_IDENT_CELUL == w_codigo_celula){
        var pessoa = getData("pes_" +pessoasCelulas[i][0].COD_IDENT_PESSO);
            pessoa = pessoa[0][0];
        w_elemento = [pessoa.TXT_NOMEX_PESSO, pessoa.COD_IDENT_PESSO, pessoasCelulas[i][0].FLG_IDENT_PESSO, pessoa.FLG_STATU_PESSO, pessoasCelulas[i][0].FLG_STATU_PARTC];
        arrayConfig.push(w_elemento);
    }

}

1 answer

3


You can do a function that looks for keys depending on the argument you give it. Something like this:

localStorage.pes_54321 = '{"baz":"biz"}';
localStorage.pes_12345 = '{"foo":"bar"}';

function getData(chave) {
    return Object.keys(localStorage).filter(function(key) {
        return key.indexOf(chave) == 0;
    }).map(function(key) {
        return JSON.parse(localStorage[key]);
    });
}
console.log(JSON.stringify(getData('pes'))); // [{"foo":"bar"},{"baz":"biz"}]
console.log(JSON.stringify(getData('pes_54321'))); // [{"baz":"biz"}]

This function returns all keys starting with the string you pass to the function. The return is an array, then you can use as needed.

jsFiddle: https://jsfiddle.net/ayw7zky4/

  • And if I pass a whole name, it returns to me only him ?

  • @Exact Renanrodrigues, an array with only one element. I joined an example.

  • Its code is returning an array within the other array, so it is becoming array[0]{ array[0] { Object, Object, ...} } How to change this ??

  • @Renanrodrigues my code generates objects within an array, not an array within an array with objects inside. Take a look at my jsFiddle console. Are you integrating into another code that is creating that problem?

  • I’m initially setting it as "[]" will that’s why ?

  • Another thing, your code is only returning one, for example if you search all pes_ it only returns the first.

  • How to do to return all ?

  • I UPDATED MY QUESTION

  • @Renanrodrigues as I put in the answer my code returns multiple if the string is common, as in the example getData('pes'). I’ll re-read your question now with the modifications.

  • @Renanrodrigues I think you are complicating, say when you are around and I can help you chat.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.