如何检查本地存储数据集中是否存在值? [英] How to check if value exist in Local Storage data set?

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

问题描述

我已经完成了一些有关本地存储的研究.似乎是存储数据的不错选择.在我的单页应用程序中,我使用JSON存储一些在我的应用程序中多次使用的数据.我想知道是否可以用本地存储替换JSON?还有什么好处呢?到目前为止,我已经能够在本地存储中加载数据,但是无法检查本地存储中是否存在值以及如何读取数据.这是示例:

I have done some research about local storage. Seems like a pretty good option to store data. In my Single Page App I use JSON to store some of the data that is used more than once in my app. I'm wondering if I can replace JSON with Local Storage? Also what would be the benefits? So far I was able to load data in Local Storage but I couldn't check if values exist in Local Storage and how I will read the data. Here is example:

$.ajax({
        type: 'POST',
        url: 'App.cfc?method='+getMethod,
        data: {'recID':recID},
        dataType: 'json'
    }).done(function(obj){
        var numRecs = obj.RECORDCOUNT;
        jsonData[recID] = obj.DATA;
        localStorage.setItem(recID,JSON.stringify(obj.DATA));

        var firstN = jsonData[recID].hasOwnProperty('F_NAME') == true ? $.trim(jsonData[recID]['F_NAME']['value']) : '',
        lastN = jsonData[recID].hasOwnProperty('L_NAME') == true ? $.trim(jsonData[recID]['L_NAME']['value']) : '';
        console.log(localStorage[recID] +'\n'+ localStorage[recID].hasOwnProperty("L_NAME")); //Check Local Storage if value exist

    }).fail(function(jqXHR, textStatus, errorThrown){
    if(jqXHR.status == 0){
        alert("Fail to connect. Please check your internet connection.");
    }else{
        alert("Error: "+errorThrown);
    }
});

以下是JSON和localStorage数据的示例:

Here is example of JSON and localStorage data:

{"F_NAME":{"value":"John"},"L_NAME":{"value":"Smith"}}

在将数据转储到控制台后,JSON和本地存储都具有相同的结构,但是在我在控制台中测试本地存储的上述代码示例中,hasOwnProperty()表示false.我想知道我的代码是否无效或其他原因导致我的代码失败.我想使用本地存储的主要原因是用户失去互联网连接时的情况.在那种情况下,我想将表单数据保存在本地存储中,这样用户就不会丢失数据.如果有人可以提供示例或任何提示的帮助,请告诉我.谢谢!

Both JSON and local Storage have the same structure after I dumped the data in the console but hasOwnProperty() indicates to false in my code example above where I test loacl storage in console. I'm wondering if my code is invalid or something else is causing my code to fail. Main reason why I would like to use local storage is situation when user loose internet connection. In that case I want to save form data in local storage that way user won;t lose the data. If anyone can provide example or help with any tips please let me know. Thank you!

推荐答案

localStorage stores strings, which you know since you're already JSON.stringify()ing when you save. But you need to replace your check of

localStorage[recID].hasOwnProperty("L_NAME")

使用

JSON.parse(localStorage[recID]).hasOwnProperty("L_NAME")

您有一个整个对象,该对象存储为localStorage[recID],因此您需要对其进行解析,然后访问生成的对象,例如:

You have a whole object which you store as localStorage[recID], so you need to parse that, then access the resulting object, like:

const record = JSON.stringify({
  "F_NAME": {
    "value": "John"
  },
  "L_NAME": {
    "value": "Smith"
  }
});

console.log(JSON.parse(record)['F_NAME']['value'])

JSON.parse(record)成为对象,然后访问其后代参数['F_NAME']['value'].括号的位置至关重要.

JSON.parse(record) becomes the object, then you access its descendant parameters ['F_NAME']['value']. The placement of the brackets is critical.

这篇关于如何检查本地存储数据集中是否存在值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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