-4

Problem: I would like get the localstorage value "car", i want to get the result like car = ["red","blue"] in other page, i want to get the result into array one by one. How can i fix it?

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;
taika chan
  • 43
  • 4
  • Do you even know what the JSON.stringify call on your first page does? if you did, you wouldn't need to ask this. – Touffy Apr 02 '17 at 15:37
  • 1
    It's very unclear what the problem is. The other element is in `car1[1]`. – JJJ Apr 02 '17 at 15:37
  • You need to put more effort into explaining the situation and specific problem. Take some time to read [ask] – charlietfl Apr 02 '17 at 15:42

1 Answers1

-3

as i know, when you convert it to json it wouldn't parse to you items like array so car[0] doesn't work as you want, This is a good resource of using localstorage: https://www.smashingmagazine.com/2010/10/local-storage-and-how-to-use-it/

U must enter the items as an object and give them an id like this:

{
 name: red,
 id: 1,
}

Then retrieve it by car id.

and a better answer: How do I store an array in localStorage?

And know it JSON is not supported in IE7 and earlier.

Community
  • 1
  • 1
Katerou22
  • 777
  • 5
  • 19