localstorage - 保存数组 [英] localstorage - save array
本文介绍了localstorage - 保存数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有本地存储工作,可以保存输入并将其推送到列表中。现在我想将列表保存在localstorage中,因为当我重新加载列表时因 var fav = new Array();
而重置列表时,在此 jsFiddle 。
感谢,
本
解决方案
//保存
这非常简单,您只需要使用JSON数据来保存即可: var datas = [1,2,3];
localStorage [datas] = JSON.stringify(datas);
//检索
var stored_datas = JSON.parse(localStorage [datas]);
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
解决方案
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"]);
这篇关于localstorage - 保存数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文