2017-04-02 33 views
-4

問題: 我想獲取localstorage值「car」,我想在其他頁面中獲得car = [「red」,「blue」]的結果,我想要將結果一個接一個地排列成數組。我該如何解決它?Javascript localstorage將值逐個獲取到數組中

page 1: 
car = ["red","blue"]; 
localStorage.setItem("car", JSON.stringify(car)); 

Another Page: 
    var car1 = JSON.parse(localStorage.getItem("car")); 
    alert(car1[0]) 
    ****show two record at the same time , i want to put it into array like car1[0] = red; car1[1] = blue; 
+0

你甚至知道第一頁上的JSON.stringify調用是幹什麼的嗎?如果你這樣做,你不需要問這個。 – Touffy

+1

這個問題很難說清楚。另一個元素在'car1 [1]'中。 – JJJ

+0

你需要付出更多努力來解釋情況和具體問題。花些時間閱讀[問] – charlietfl

回答

-3

,因爲我知道,當你將其轉換成JSON,將不能解析到你的項目,如數組 所以只要你想car[0]不起作用, 這是使用本地存儲的一個很好的資源: https://www.smashingmagazine.com/2010/10/local-storage-and-how-to-use-it/

ü必須輸入的項目作爲對象,並給他們一個ID 這樣的:

{ 
name: red, 
id: 1, 
} 

然後乘車ID檢索。

和一個更好的答案:How do I store an array in localStorage?

,知道它在IE7不支持和更早JSON。