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

Angular's new httpResource and a bonus hidden feature

  The first   release candidate   for   Angular 19.2   is out, and it includes the new experimental   httpResource . This is essentially a s...

Best for you