在更复杂的情形合并的localStorage(保存多个阵列) [英] Incorporating localStorage in a more complicated scenario (save multiple arrays)
问题描述
我想实现本地存储到我的程序。
I'm trying to implement local storage into my program.
首先,程序简单,只有1阵列进行保存。该数组担任容器,然后容器输送到#1 - 显示输出和#2 - 控制台输出...
Initially, the program was simple and had only 1 array to save. That array served as the container and the container then fed to #1 - the display output and to #2 - the console output...
pseudo-code to make the red box go away...
上面的链接,最终工作 - 因为你可以在JS的54线看,一旦一个新条目输入到输入框中,更新的容器然后保存到localStorage的。然后在返回到现场,每行11 - 如果在程序的初始加载保存的localStorage的容器不初始阵列匹配,则使用保存的容器
The above link eventually worked - as you can see on line 54 of the JS, as soon as a new entry is entered into the input box, the updated container was then saved to localStorage. And then upon returning to the site, per line 11 - if upon the initial loading of the program the saved localStorage container doesn't match the initial array, then the saved container is used.
不过,我已经彻底改变了计划,因此现在有多种可供选择的方案,因此多个阵列。取其选项/阵列被选择成为主容器中,并在主容器仍然送出到显示输出和控制台输出。请参阅下面的更新的程序...
However, I've changed the program drastically so that there are now multiple options to choose from and thus multiple arrays. Whichever option/array is chosen becomes the main container, and the main container still feeds out to the display output and the console output. See the updated program below...
pseudo-code
问题是,我现在有困难的时候搞清楚如何实现这个新的更新的程序localStorage的。这是很容易previously时,我只用了1阵列要测试的。现在,我不能只保存容器的新条目输入(或删除),因为容器可以是任何的6个选项1的任何时间。
The problem is that I am now having a difficult time figuring out how to implement localStorage on this new updated program. It was much easier previously when I only had 1 array to test against. Now, I can't just save the container any time a new entry is inputted (or removed) because the container can be any 1 of the 6 options.
没有人有任何总体思路/指针/建议,以我能做些什么?
Does anyone have any general ideas/pointers/suggestions as to what I can do?
推荐答案
最简单的(也许不是最有效的)选择是使用数组的数组。如果你收到:
The easiest (maybe not the most efficient) option would be to use an array of arrays. If before you had:
var container = ["a", "b", "c"];
localStorage.setItem("container", JSON.stringify(container));
现在不得不
var container = [
["a", "b", "c"],
["d", "e", "f"]
];
localStorage.setItem("container", JSON.stringify(container));
然后获取在新的code单一的,你只是做:
Then for getting a single one in your new code, you'd just do:
var container = JSON.parse(localStorage.getItem('container')); //The same as before
var firstItem = container[0];
希望这有助于。干杯
Hope this helps. Cheers
这篇关于在更复杂的情形合并的localStorage(保存多个阵列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!