FOR no localstorage does not work

Asked

Viewed 27 times

0

Good night, you guys,

The state of my LOCALSTORAGE is like this:

inserir a descrição da imagem aqui

When I try to do that:

for (var post in $window.localStorage) {
                console.log(post);
                var value = JSON.parse($window.localStorage[post]);
                alert(value);
                response.push(value)
            }

It even takes the length attribute that already comes with Storage and the key attribute that I don’t know where it came from.

inserir a descrição da imagem aqui

1 answer

2

Before the loop you need to convert to JSON using the localStorage name with .getItem:

var json = JSON.parse(window.localStorage.getItem('cadeira-maia'));

Inside the loop you take the values of each item:

var json = JSON.parse(window.localStorage.getItem('cadeira-maia'));
for (var post in json) {
   console.log(post);
   alert(json[post]);
   response.push(value)
}

Browser other questions tagged

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