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.
ACCORDING TO WHAT IS IN MY LOCALSTORAGE SHOULD RETURN 3 RECORD.
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);
}
}
And if I pass a whole name, it returns to me only him ?
– Renan Rodrigues
@Exact Renanrodrigues, an array with only one element. I joined an example.
– Sergio
Its code is returning an array within the other array, so it is becoming array[0]{ array[0] { Object, Object, ...} } How to change this ??
– Renan Rodrigues
@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?
– Sergio
I’m initially setting it as "[]" will that’s why ?
– Renan Rodrigues
Another thing, your code is only returning one, for example if you search all pes_ it only returns the first.
– Renan Rodrigues
How to do to return all ?
– Renan Rodrigues
I UPDATED MY QUESTION
– Renan Rodrigues
@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.– Sergio
@Renanrodrigues I think you are complicating, say when you are around and I can help you chat.
– Sergio
Let’s go continue this discussion in chat.
– Sergio