Not able to compare keys

I have the following function: The value of particularKey I am getting in console log is title

And console.log(key); prints the following JSON

{“logged_user”:“myname”,“title”:“Reports”,“Data”:“30”}

In the loop, I am always getting Not Matched. Why if(key === particularKey) is not matching with the particularKey that I am trying to compare?

this.removeWebStorageKey = function(particularKey){
      
        console.log("Length of localstorage BEFORE  "+localStorage.length);
        console.log("Particular Key we are getting");
        console.log(particularKey);  
        var key; 
        key = localStorage.getItem(localStorage.key(0));
        console.log(key);
        console.log("Printing Key Outside loop");
        

        if(key === particularKey)
                { 

                    console.log("YES it matched!");
                 }
             else {
                console.log("Not matched!");
             }    
    

    }

well, one of these things is a string, and the other is an object. So no, they will never match.

Based on the JSON, you’re probably trying to compare particularKey to key.myname

I am noticing one strange thing:

I see everything fine with the following code in JSFiddle here

var rfekey = {"logged_user":"myname","abcDatatitle":"AHarmbca Reports","abcData":"300"};

console.log(rfekey);


console.log(Object.keys(rfekey).length);

for (const key of Object.keys(rfekey)) {
  console.log("Printing Keys Only "+key);
}

image

But in my code, for the same code, it’s converting it into array characterwise I believe and I am seeing following output:

image

It goes all the way to 74th. Instead of displaying length 3, it’s just like it displays in JSFiddle, I am getting the length as 74. It looks like it’s counting characters.

Do youknow why that’s happening? Could it be because I am using key = localStorage.getItem(localStorage.key(0)); for HTML5 storage?

well it’s not the same code.

In the first bit of code, you’re referencing a specific object.

In the second bit of code, you’re pulling out whatever is the 0th item in localstorage and reading it.

Are you certain that the 0th item in localstorage is the one you want to be reading? How?

Hey @m_hutley,

When I placed the following code for testing :

        console.log("December 29th Test");
        var s = localStorage.getItem(localStorage.key(0));
        console.log(s);

I only saw the following :

December 29th Test 

{"logged_user":"myname","abcDatatitle":"AHarmbca Reports","abcData":"300"};

Interestingly, I don’t see Object word before it just like I see it in the JSFiddle console log which is as shown below:

image

So, should I convert my variable s (
var s = localStorage.getItem(localStorage.key(0));)to object first and then try to achieve what I am trying to? OR what do you think would be the best strategy? Thanks!

the {} notation denotes an Object in JSON output format. The “Object” word is just your browser rendering it out explicitly for visual sake.

What precisely are you trying to compare? Give me an example of what you would:
A: Pass to your function,
B: Expect it to compare and return a result on.

@m_hutley I see. So, basically, I want to remove a particular key from the local storage. I don’t want to clear everything from the local storage but only one key which is abcDatatitle.

So, as shown in the function below if my function receives abcDatatitle which is the second key after logged_user in my json object, I want to remove it.

For example, I am assuming the JSON object will look like this after the key abcDatatitle is removed

Original Json Object: {"logged_user":"myname","abcDatatitle":"AHarmbca Reports","abcData":"300"};

After Removal : {"logged_user":"myname","abcData":"300"};


this.removeWebStorageKey = function(particularKey){
      
      var key; 
      key = localStorage.getItem(localStorage.key(0));
    
        

        if(key === particularKey)
                { 

                    console.log("YES it matched!");
                    localStorage.removeItem(particularKey);
                 }
             else {
                console.log("Not matched! Do Nothing!");
             }    
    

    }

Please let me know if I can answer more questions. Thanks !

Secondly, why browser didn’t display Object in my locaStorage scenario ?

So there’s a confusion of terms here.

the key in the local storage is unknown (you never write it to a location you can find). That key refers to the entire object that you’ve listed there. If you’re trying to remove the key from local storage, you would obliterate the entire element by saying localStorage.removeItem(localStorage.key(0)) and be done with it.

What you instead appear to be wanting to do is to remove a property from the object at index 0 of the localstorage array. I will again caution you that assuming your target value is at position 0 is a dangerous one.

You’ve found the object, and stored it as key.

Next (rather unnecessary) step you appear to do is determine if the object in question contains the property defined by particularKey. Objects have a method for that: if(key.hasOwnProperty(particularKey)) {

Then you want to remove only that particular property of the object. That’s the delete operator. delete key[particularKey]

Then, because you’ve done all this on a local version of the object, you need to store that information back into localstorage, so that it updates it.
localStorage.setItem(localStorage.key(0),key);

1 Like

Thanks for the clarifications. When I used the following logic,

if(key.hasOwnProperty( particularKey))
                { 

                    console.log("YES it matched!");
                 }
             else {
                console.log("Not matched!");
             }

I was still getting Not matched which is weird.

I tried hardcoding the keyname as well like this

if(key.hasOwnProperty("abcDatatitle")) but no change.

Things works fine as expected in this JSFiddle http://jsfiddle.net/axkdmwgy/4/

console.log(typeof key)

Incidentally, the entire if is (mostly) redundant, because delete works whether or not the object has the specified key (it just doesnt delete anything if it doesnt.)

This is printing string . Do you think this could be the problem?

And, I tried testing it like the following:

console.log(delete key[" abcDatatitle"]);
console.log(delete key.abcDatatitle);

Both of the statements returned true and when I printed the key , I saw that it wasn’t deleted.

Converting key into an object using var jsonObject = JSON.parse(key); and then performing deletion works as expected since this is an object. However, if I want to perform the same operation on the string({“logged_user”:“myname”,“abcDatatitle”:“AHarmbca Reports”,“abcData”:“300”};), how should I go about it? Thanks

I have got it working. Basically, I had to convert it to object, then remove the key and then convert it back to string and then update the localstorage.

Thanks @m_hutley for your time in troubleshooting this with me. Appreciated.

1 Like

the console did you a disservice by not putting quotes around the string, which would have been the giveaway as to the problem. What you came up with is the correct solution. Parse it to object, delete the property, and stringify it for storage once again.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.