How to recover data from localstore?

Asked

Viewed 48 times

-5

How do I retrieve items that are this way in localStore

lista-pessoas[{"name":"Hamburguer","adicao":"+carne","data":"2"}]
  • Add more details to your question so it can be answered. If in doubt, please read the Stackoverflow Survival Guide https://pt.meta.stackoverflow.com/questions/8045/guia-survivor%C3%aancia-do-stack-overflow-em-inglés%C3%aas? cb=1

  • I would like to list them for example:

  • Item: name, name, name

  • List in a div

2 answers

0

0


To redeem the item from localStorage use the method getaway and how a text has been saved and this represents an object as demonstrated in your question needs to be converted again to an object with JSON.parse(), a complete example follows below with recording, recovery and viewing information in a list:

//dados.
const data = [
    {"name":"Hamburguer 1","adicao":"+carne","data":"2"},
    {"name":"Hamburguer 2","adicao":"+carne","data":"2"},
    {"name":"Hamburguer 3","adicao":"+carne","data":"2"}
];

//gravando o dado no localStorage.
localStorage.setItem('data', JSON.stringify(data));

//recuperando o dado do localStorage.
const textData = localStorage.getItem('data');
const newData = JSON.parse(textData);

//recuperando a tag da sua tela.
var ul = document.getElementById('tul');

//listando a informação.
newData.map(function(data, index) {
    const li = document.createElement('li');
    li.appendChild(
        document.createTextNode(data.name)
    )   
    ul.appendChild(li);
});
  • great and how I display in html in a div?

  • I got it, bro! Topppp

  • Only one question how to save the items for example a link that saves these changes? for example <the name date="BURGER1" value date="5.00">BURGER1</a> <the name date="+MEAT" value date="1.00">+MEAT</a> <the name date="+LETTUCE" value date="1.00">+LETTUCE</a>

Browser other questions tagged

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