0

Im trying to save a simple array [5,2] on Local Storage and then collecting it . i save it succesfully but when im trying to collect it using "JSON.parse" it didn't work and show me this error : "Uncaught SyntaxError: Unexpected token m in JSON at position 0 at JSON.parse () at HTMLAnchorElement.addo"

I trying to rewrite many code on many ways , but the same error appeared

numbers=new Array();
numbers.push(5);
numbers.push(2);
localStorage.setItem("items",numbers);
let data=localStorage.getItem("items");
data=JSON.parse(data);
console.log(data);

i except that i will had my previous array , but i had an error .

Eilon
  • 25,582
  • 3
  • 84
  • 102

1 Answers1

2
numbers=new Array();
numbers.push(5);
numbers.push(2);
localStorage.setItem("items",numbers);

If you inspect localStorage.items at this point, you'll see it's the string "5,2"

Try this instead:

numbers=new Array();
numbers.push(5);
numbers.push(2);
localStorage.setItem("items",JSON.stringify(numbers));
TKoL
  • 13,158
  • 3
  • 39
  • 73