简体   繁体   English

如何检查本地存储数据集中是否存在值?

[英]How to check if value exist in Local Storage data set?

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. 在我的单页应用程序中,我使用JSON存储一些在我的应用程序中多次使用的数据。 I'm wondering if I can replace JSON with Local Storage? 我想知道是否可以用本地存储替换JSON? 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);
    }
});

Here is example of JSON and localStorage data: 这是JSON和localStorage数据的示例:

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

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. 我在控制台中转储数据后,JSON和本地存储都具有相同的结构,但是hasOwnProperty()在我在控制台中测试本地存储的上述代码示例中指示false 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. localStorage存储字符串 ,由于您在保存时已经JSON.stringify() ,所以您会知道这些字符串 But you need to replace your check of 但是您需要更换支票

localStorage[recID].hasOwnProperty("L_NAME")

with

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

Edit: You have a whole object which you store as localStorage[recID] , so you need to parse that, then access the resulting object, like: 编辑:您有一个完整的对象 ,将其存储为localStorage[recID] ,因此您需要对其进行解析,然后访问生成的对象,例如:

 const record = JSON.stringify({ "F_NAME": { "value": "John" }, "L_NAME": { "value": "Smith" } }); console.log(JSON.parse(record)['F_NAME']['value']) 

JSON.parse(record) becomes the object, then you access its descendant parameters ['F_NAME']['value'] . JSON.parse(record)成为对象,然后访问其后代参数['F_NAME']['value'] The placement of the brackets is critical. 括号的位置至关重要。

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

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