How do I store an array in localStorage? With Demo

 localStorage only supports strings. Use JSON.stringify() and JSON.parse().

var names = [];
names[0] = prompt("New member name?");
localStorage.setItem("names", JSON.stringify(names));

//...var storedNames = JSON.parse(localStorage.getItem("names"));

  You can also use direct access to set/get item:

localstorage.names = JSON.stringify(names);
var storedNames = JSON.parse(localStorage.names);

Demo URl 

https://stackblitz.com/edit/js-k29afs?file=index.js

Fig.1



No comments:

Post a Comment

CPU vs GPU Architecture

  CPU vs GPU Architecture CPU (Central Processing Unit) and GPU (Graphics Processing Unit) have distinct architectural differences, optimize...

Best for you