Recover value in localstorage

Asked

Viewed 75 times

0

Good afternoon! I am creating a Javascript application using localstorage for data storage. The application is simple, I’m just practicing some concepts, and it involves some personal data and a field composed of 10 numbers as unique identification, generated when registering. What I’m struggling to create is a function that looks for this unique identification amid data saved in localstorage.

function pesquisarAluno(){
let matriculas = Array()
let id = localStorage.getItem('id')
for(let i = 1; i <= id; i++){
    let recuperar_matricula = JSON.parse(localStorage.getItem(i))
    if(recuperar_matricula === null){
        continue
    }
    recuperar_matricula.id = i
    matriculas.push(recuperar_matricula)
}
console.log(matriculas)

}

  • And what is the difficulty? What is the structure of the saved data?

  • At the moment I am able to pass the data to an array, however I am not able to recover any registration based on the registration. Basically it is to recover a record through this identification.

1 answer

0

To work with Storage you need to set your data in your Storage and then you can recover the data.

to set a Storage Location you can do as below:

localStorage.setItem("alunos", JSON.stringify(DADOS));

to recover you can utilize

localStorage.getItem("alunos")

your question was not well defined, try to better describe your problem.

an example of a working code with Storage, I applied inside an html file to be able to run in a browser.

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0" 
 />
 <title>Document</title>
</head>
<body>
<script>
  // defino um array de obetos
  const alunos = [
    {
      id: 1,
      nome: "Fulano de Tal Tal Tal",
    },
    {
      id: 2,
      nome: "Beltrado de Tal Tal Tal",
    },
    {
      id: 3,
      nome: "Melano de Tal Tal Tal",
    },
  ];

  // defino o storage
  const defineStorage = () => {
    localStorage.setItem("alunos", JSON.stringify(alunos));
  };

  // executo a função
  defineStorage();

  // defino uma função de recuperação do storage
  const recebeStorage = () => {
    return JSON.parse(localStorage.getItem("alunos"));
  };
  // converto em um array para trabalhar com map, filter ou outros ajudadores dos arrays
  const storage = Array.from(recebeStorage());

  // função para filtro
  const id = (id) => id.id === 2;
  
  // aplicando a função e imprimindo no console
  console.log(storage.filter(id));
  
</script>
  • I’m really stuck on this Torage thing. If I apply a console right after the loop, I can pull the ID’s in the array, what I want is to pull a specific data inside that array, you know? It’s complicated

  • Don’t cut way, understand all of array, then you will see how easy this is. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

Browser other questions tagged

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