Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

localstorage - save array [duplicate]

I have localstorage working to where I can save inputs and push them to a list. Now I would like to save the list in localstorage because when I reload the list resets because of var fav = new Array(); is defined at the start in this jsFiddle. How can I work around this?

Thanks, Ben

like image 617
benjipelletier Avatar asked Apr 10 '14 15:04

benjipelletier


People also ask

Can you save an array in localStorage?

Save Array In localStorageYou can save the array by using the setItem method. const myBlogs = ["https://catalins.tech", "https://exampleblog.com"]; localStorage. setItem('links', JSON. stringify(myBlogs));

Can localStorage store array of objects?

The localStorage API in browsers only supports adding information in a key:pair format and the key and the pair should be of type string thus native objects or arrays cannot be stored in the localStorage .

What does localStorage Clear () do?

The clear() method of the Storage interface clears all keys stored in a given Storage object.


1 Answers

It's realy easy, you just need to use JSON data to save it :

// Save
var datas = ["1", "2", "3"];
localStorage["datas"] = JSON.stringify(datas);

// Retrieve
var stored_datas = JSON.parse(localStorage["datas"]);
like image 177
Arthur Avatar answered Sep 25 '22 01:09

Arthur