dynamic array to local storage/jstorage?

大兔子大兔子 提交于 2019-12-06 15:28:30

You can use json object for this like,

localStorage.setItem('names', {'ABC','XYZ'}); //and so on

or json string like

var json={'ABC','XYZ'};
localStorage.setItem('names', JSON.stringify(json)); //and so on

To get an existing item try this,

console.log(localStorage.getItem('names'));

Read this https://developer.mozilla.org/en-US/docs/Web/Guide/DOM/Storage

You have to JSON.stringify and JSON.parse your array to store and load it respectively.

// initialize
var myArray = [];
// load saved array
if(window.localStorage["savedArray"] != null)
    myArray = JSON.parse(window.localStorage["savedArray"]);
// modify array
myArray.push("abc123");
// re-save array
window.localStorage["savedArray"] = JSON.stringify(myArray);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!