bryan bryan - 3 months ago 8
AngularJS Question

Javascript Array not showing up as an array

I have an array like so:

var data = {"result":"success","ids":["00000","54321","123","22222","11111","55555","33333","abc123","123abc","12345","44444"]}
localStorage.ids = data.ids;


But now when I do:

angular.forEach(localStorage.ids, function(id, key) {
console.log(id);
});


I get like:

0
0
0
0
0
,
5
4
3


And so on.

When I
console.log(JSON.stringify(localStorage.ids));
I get:

"00000,54321,123,22222,11111,55555,33333,abc123,123abc,12345,44444"


Does anyone know why this would happen?

Answer

localStorage stores only strings.

localStorage.setItem('ids', JSON.stringify(data.ids)); // to save

var ids = JSON.parse(localStorage.getItem('ids')); // to get