简体   繁体   English

将阵列保存在本地存储中

[英]Save array in local storage

This is my full code 这是我的完整代码

I need to save the array visited to local storage. 我需要将访问的阵列保存到本地存储。 Then, I need an if statement to check if the array has been stored. 然后,我需要一个if语句来检查数组是否已存储。 If it has, it will do 如果有,它将做

return;

ending the code and making the button not functional. 结束代码并使按钮不起作用。 Something like this; 像这样的东西;

if (store.length == 3) {
    document.getElementById('btn').className = 'maxques';
    alert('You have completed this category');
    console.log(store);
    return; }

I just somehow need to store the array. 我只是需要存储数组。 I tried JSON stringify followed by JSON parse but either they don't work or I'm doing them wrong. 我尝试了JSON stringify,然后进行JSON解析,但是它们要么不起作用,要么我做错了。 Any ideas? 有任何想法吗?

At the risk of repeating @Tom Hart, use a combination of the functions localstorage.setItem and localStorage.getItem. 冒着重复@Tom Hart的风险,请结合使用localstorage.setItem和localStorage.getItem函数。 Respectively params being key, value and key. 参数分别是键,值和键。

To store your array: 要存储数组:

localStorage.userEdits=array.join(","); //or another delimiter

To check if stored: 要检查是否存储:

if(localStorage.userEdits){
    //stored!
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM