2
I am trying to recover a JSON that I saved in localStorage, in function salvarOffline()
. With the code as is the function listarHorarios()
I get a undefined
and the loop loop does not work. My code:
function salvarOffline(data) {
window.localStorage.setItem("horariosOffline", JSON.stringify(data));
}
function listarHorarios() {
listaFormatada = JSON.parse(localStorage.getItem('horariosOffline'));
var tabelaHorarios = document.querySelector('.horarios');
for(var i = 0; i < listaFormatada.horarios.length; i++) {
var linhaHorario = document.createElement('tr');
linhaHorario.innerHTML =
'<td class="mdl-data-table__cell--non-numeric">' + listaFormatada[i].linha + '</td>' +
'<td class="mdl-data-table__cell--non-numeric">' + listaFormatada[i].partida + '</td>' +
'<td class="mdl-data-table__cell--non-numeric"><strong>' + listaFormatada[i].chegada + '</strong></td>';
tabelaHorarios.appendChild(linhaHorario);
}
}
The structure of my JSON is:
{
"horarios": [
{
"id": 1,
"linha": "Avenida",
"horario": {
"partida": "7:20",
"chegada": "7:40"
}
},
{
"id": 2,
"linha": "St. Antônio | Avenida | S. Sebastião",
"horario": {
"partida": "7:25",
"chegada": "8:00"
}
}
]
}
The problem is you’re calling
listaFormatada[i]
when it was supposed to belistaFormatada.horarios[i]
... see working in stackblitz– NoobSaibot
Really worked, thanks @wmsouza !
– Luan