2016-09-04 62 views
0

我有一個數組,像這樣:JavaScript數組沒有顯示出來,作爲一個數組

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

但現在,當我做:

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

我得到這樣的:

0 
0 
0 
0 
0 
, 
5 
4 
3 

而且等等。

當我console.log(JSON.stringify(localStorage.ids));我得到:

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

有誰知道爲什麼會發生這種情況?

+0

在我看來,像是你省略的某些代碼在forEach循環之前刺痛了localStorage.ids,或者angular的forEach沒有按預期工作。 – theonlygusti

回答

1

localStorage只存儲字符串。

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

var ids = JSON.parse(localStorage.getItem('ids')); // to get 
相關問題