如何将数组存储在html5的localStorage对象中? [英] How to store array in localStorage Object in html5?

查看:74
本文介绍了如何将数组存储在html5的localStorage对象中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将mycars数组存储在html5的localStorage对象中?


$ b

  var mycars = new Array (); 
mycars [0] =萨博;
mycars [1] =沃尔沃;
mycars [2] =宝马;

localStorage.mycars = ?;


解决方案

localStorage 用于 key:value 对,所以你可能想要做的是 JSON.stringify the数组并将字符串存储在 mycars 键中,然后您可以将其取出并放回 JSON.parse 它。例如,

  var mycars = new Array(); 
mycars [0] =萨博;
mycars [1] =沃尔沃;
mycars [2] =宝马;

localStorage [mycars] = JSON.stringify(mycars);

var cars = JSON.parse(localStorage [mycars]);


How to store mycars array in localStorage Object in html5?

var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";

localStorage.mycars=?;

解决方案

localStorage is for key : value pairs, so what you'd probably want to do is JSON.stringify the array and store the string in the mycars key and then you can pull it back out and JSON.parse it. For example,

var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";

localStorage["mycars"] = JSON.stringify(mycars);

var cars = JSON.parse(localStorage["mycars"]);

这篇关于如何将数组存储在html5的localStorage对象中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆