本文翻译自:How do I store an array in localStorage? [duplicate]
This question already has an answer here: 这个问题已经在这里有了答案:
- Storing Objects in HTML5 localStorage 19 answers 在HTML5 localStorage中存储对象 19个答案
If I didn't need localStorage, my code would look like this: 如果我不需要localStorage,我的代码将如下所示:
var names=new Array();
names[0]=prompt("New member name?");
This works. 这可行。 However, I need to store this variable in localStorage and it's proving quite stubborn. 但是,我需要将此变量存储在localStorage中,事实证明它很顽固。 I've tried: 我试过了:
var localStorage[names] = new Array();
localStorage.names[0] = prompt("New member name?");
Where am I going wrong? 我要去哪里错了?
#1楼
参考:https://stackoom.com/question/E5S5/如何在localStorage中存储阵列-重复
#2楼
The localStorage
and sessionStorage
can only handle strings. localStorage
和sessionStorage
只能处理字符串。 You can extend the default storage-objects to handle arrays and objects. 您可以扩展默认的存储对象以处理数组和对象。 Just include this script and use the new methods: 只需包含此脚本并使用新方法即可:
Storage.prototype.setObj = function(key, obj) {
return this.setItem(key, JSON.stringify(obj))
}
Storage.prototype.getObj = function(key) {
return JSON.parse(this.getItem(key))
}
Use localStorage.setObj(key, value)
to save an array or object and localStorage.getObj(key)
to retrieve it. 使用localStorage.setObj(key, value)
保存一个数组或对象,并使用localStorage.getObj(key)
检索它。 The same methods work with the sessionStorage
object. 相同的方法适用于sessionStorage
对象。
If you just use the new methods to access the storage, every value will be converted to a JSON-string before saving and parsed before it is returned by the getter. 如果仅使用新方法访问存储,则每个值将在保存之前进行转换并转换为JSON字符串,并在由getter返回之前将其解析。
Source: http://www.acetous.de/p/152 资料来源: http : //www.acetous.de/p/152
#3楼
The JSON approach works, on ie 7 you need json2.js, with it it works perfectly and despite the one comment saying otherwise there is localStorage on it. JSON方法可行,即在7上,您需要json2.js,它可以完美运行,尽管有一条评论说否则上面有localStorage。 it really seems like the best solution with the least hassle. 确实看起来是麻烦最少的最佳解决方案。 Of course one could write scripts to do essentially the same thing as json2 does but there is little point in that. 当然,可以编写脚本来执行与json2基本上相同的操作,但是这样做没有什么意义。
at least with the following version string there is localStorage, but as said you need to include json2.js because that isn't included by the browser itself: 4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; BRI/2; NP06; .NET4.0C; .NET4.0E; Zune 4.7) (I would have made this a comment on the reply, but can't). 至少在以下版本字符串中存在localStorage,但如上所述,您需要包含json2.js,因为浏览器本身不包含该文件:4.0(兼容; MSIE 7.0; Windows NT 6.1; WOW64; Trident / 5.0; SLCC2 ; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; BRI / 2; NP06; .NET4.0C; .NET4.0E; Zune 4.7)(我会对此发表评论)回复,但不能)。
#4楼
Just created this: 刚刚创建了这个:
https://gist.github.com/3854049 https://gist.github.com/3854049
//Setter
Storage.setObj('users.albums.sexPistols',"blah");
Storage.setObj('users.albums.sexPistols',{ sid : "My Way", nancy : "Bitch" });
Storage.setObj('users.albums.sexPistols.sid',"Other songs");
//Getters
Storage.getObj('users');
Storage.getObj('users.albums');
Storage.getObj('users.albums.sexPistols');
Storage.getObj('users.albums.sexPistols.sid');
Storage.getObj('users.albums.sexPistols.nancy');
#5楼
localStorage
only supports strings. localStorage
仅支持字符串。 Use JSON.stringify()
and JSON.parse()
. 使用JSON.stringify()
和JSON.parse()
。
var names = [];
names[0] = prompt("New member name?");
localStorage.setItem("names", JSON.stringify(names));
//...
var storedNames = JSON.parse(localStorage.getItem("names"));
#6楼
Use JSON.stringify()
and JSON.parse()
as suggested by no! JSON.stringify()
建议使用JSON.stringify()
和JSON.parse()
! This prevents the maybe rare but possible problem of a member name which includes the delimiter (eg member name three|||bars
). 这样可以防止包含分隔符的成员名称(例如,成员名称three|||bars
)可能罕见但可能出现的问题。