I have this code:
let myArray= [{ gained: 0, lost: 0 }]; localStorage.setItem('allPoints', JSON.stringify(myArray));
This works fine but if I want to parse it
localStorage.getItem('allPoints', JSON.parse(allPoints));
I get following error:
Uncaught SyntaxError: “[object Object]” is not valid JSON
Can’t you parse an array like this? Is there a way around this?
Advertisement
Answer
You definitely can.
You only got it a bit wrong with getItem.
It should be:
JSON.parse(localStorage.getItem("allPoints"))
You first need to retrieve the value. You might want to check also https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem.
And then you apply JSON.parse