Javascript localstorage逐个将值放入数组中

时间:2017-04-02 15:35:03

标签: javascript arrays storage frontend local

问题: 我想获得localstorage值" car",我想得到的结果如car = [" red"," blue"]在其他页面,我想要将结果逐个放入数组中。我该如何解决?

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;

1 个答案:

答案 0 :(得分:-3)

据我所知,当你将它转换为json时,它不会解析像数组这样的项目 所以car[0]并不能按你的意愿工作, 这是使用localstorage的好资源:  https://www.smashingmagazine.com/2010/10/local-storage-and-how-to-use-it/

U必须输入项目作为对象并给他们一个id 像这样:

{
 name: red,
 id: 1,
}

然后通过车识别来检索它。

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

并且知道IE7及更早版本不支持JSON。