I am currently trying to store an array into sessionStorage
and then retrieve the data from sessionStorage
. Then, store the sessionStorage
data back into an array.
var testArray = ["Shirt", "Bottom", "Shoes"];
window.sessionStorage.setItem("items", JSON.stringify(testArray));
var storedArray = JSON.parse('[' + sessionStorage.getItem("items") + ']');
var i;
for (i = 0; i < storedArray.length; i++) {
alert(storedArray[i]);
}
Am I doing anything wrong here?
It is already stored as an array, you don't need the brackets. What you are doing is putting the original array in a new array.
try this:
var testArray = ["Shirt", "Bottom", "Shoes"];
window.sessionStorage.setItem("items", JSON.stringify(testArray));
var storedArray = JSON.parse(sessionStorage.getItem("items"));//no brackets
var i;
for (i = 0; i < storedArray.length; i++) {
alert(storedArray[i]);
}
https://jsfiddle.net/517x5rcg/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With