如何在localstorage中存储数组数组? [英] how to store array of array in localstorage ?
本文介绍了如何在localstorage中存储数组数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在localstorage中存储大小不同的数组数组?
解决方案
本地存储基于名称/值对,与所有关联容器一样在Javascript(基本上所有Javascript对象,包括数组,都是这样的关联数组):
http:/ /en.wikipedia.org/wiki/JavaScript#Dynamic [ ^ ],
http://en.wikipedia.org/wiki/Associative_arrays [ ^ ]。
请查看它的工作原理: http://diveintohtml5.info/storage.html [ ^ ]。
关联容器没有特定大小:随着您添加数据,大小会增加。让我们看看它如何与整数索引的单个数组一起工作:
localStorage [ 10 ] = 值@ 10;
// 如果您的本地存储空前,您只有一个元素(不是11个或10个) )
localStorage [ 111 ] = 值@ 111;
// 现在只有两个元素
alert(localStorage) [ 10 ]); // 将显示值@ 10
同样,您可以拥有数组数组,您可以简单地将其解释为使用复合索引索引的数组,在这种情况下,它可以只是一个对索引:
localStorage [ 1 , 13 ] = 值@ [1,13];
localStorage [ 111 ] = 值@ 112\" 跨度>; // 您甚至可以组合1D和2D数组
alert(localStorage [ 1 , 13 ]); // 将显示值@ [1,13]
根据定义,您将获得比预期更多的灵活性:您将获得锯齿状数组: http://en.wikipedia.org/wiki/Jagged_array [ ^ ]。
这就是全部。
-SA
how can i store array of array in localstorage which are different in size?
解决方案
The local storage is based on name/value pairs, like all associative containers in Javascript (essentially all Javascript object, including arrays, are such associative arrays):
http://en.wikipedia.org/wiki/JavaScript#Dynamic[^],
http://en.wikipedia.org/wiki/Associative_arrays[^].
Please see how it works: http://diveintohtml5.info/storage.html[^].
The associated container does not have certain size: the size grows as you add data. Let's see how it works with a single array indexed by integers:
localStorage[10] = "the value @10"; // if your local storage was empty before, you have only one element (not 11 or 10 of them) localStorage[111] = "the value @111"; // only two elements for now alert(localStorage[10]); // will display "the value @10"
Likewise, you can have array of arrays, which you can simply interpret as the array indexed with compound index, which can be, in this case, just a pair of indices:
localStorage[1,13] = "the value @[1,13]"; localStorage[111] = "the value @112"; // you can even combine 1D and 2D arrays alert(localStorage[1,13]); // will display "the value @[1,13]"
By definition, you will have more flexibility than you expected: you will get jagged arrays: http://en.wikipedia.org/wiki/Jagged_array[^].
That's all.
—SA
这篇关于如何在localstorage中存储数组数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文