How to use Boolean in Json

0

I have the following JSON, and inside my code I have a check to see if autoLogin is enabled.

Object {autoLogin: "true", 
     autoLoginKey: "bWF4LnJvZ2VyaW8=&YWU4NjIwMGJhMTU0NWQzMjQ0ZmRlM2FhYWNiYTJjZmM="}

However to do this check use the value of autoLogin , but I have to check it as string.

if(key.autoLogin === 'true'){//...code}

How do I instead of comparing as string, compare how boolean itself.

Type:

if(key.autoLogin === true){//code...}
    
asked by anonymous 05.08.2017 / 01:40

1 answer

0

Based on what you indicated in the comment, and if you are using localStorage to save the value of autoLogin with true to localStorage , it is saved as string . To be more accurate as a DOMString , which is a string in UTF-16 and which is mapped directly to string .

Something we can see at setItem documentation

This means that it will be saved as a string and later also obtained as a string through getItem ( documentation ).

You could of course try to convert the value from getItem to boolean to make the comparison, using for example JSON.parse :

if (JSON.parse(key.autoLogin) === true){

Or even make a custom function to convert to boolean , although this is worse than doing the condition you are already doing at this time, such as string :

if (key.autoLogin === 'true'){//...code}
    
05.08.2017 / 03:18