Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to boolean in typescript Angular 4

I know am not the first to ask this and as I mentioned in my title ,I am trying to convert string value boolean .

I have previously put some values into local storage,Now I want to get all the values and assign all to the some boolean variables .

app.component.ts

localStorage.setItem('CheckOutPageReload', this.btnLoginNumOne + ',' + this.btnLoginEdit);

here this.btnLoginNumOne and this.btnLoginEdit are string values ("true,false").

mirror.component.ts

if (localStorage.getItem('CheckOutPageReload')) {
      let stringToSplit = localStorage.getItem('CheckOutPageReload');
      this.pageLoadParams = stringToSplit.split(',');

      this.btnLoginNumOne = this.pageLoadParams[0]; //here I got the error as boolean value is not assignable to string
      this.btnLoginEdit = this.pageLoadParams[1]; //here I got the error as boolean value is not assignable to string
}

in this component this.btnLoginNumOne and this.btnLoginEdit are Boolean values;

I tried the solutions in stackoverflow but nothing is worked.

Can anyone help me to fix this .

like image 843
Zhu Avatar asked Aug 25 '18 13:08

Zhu


People also ask

How do you change a string to a boolean in typescript?

To convert a string to a boolean in TypeScript, use the strict equality operator to compare the string to the string "true" , e.g. const bool = str === 'true' . If the condition is met, the strict equality operator will return the boolean value true , otherwise false is returned. Copied!

Can you convert string to boolean?

To convert String to Boolean, use the parseBoolean() method in Java. The parseBoolean() parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".

How to convert string to Boolean in typescript?

There are multiple following ways to convert String to boolean in typescript. For example, a String object holds a true or false value We will use a string equality check to convert this to A Boolean. The only disadvantage of this approach is that it returns false for the string values ‘false’ and ‘asdfasdfasd’ string values.

How to convert boolean string values to string format in JSON?

It is one more way of converting boolean string values to string format. The JSON parse method accepts a string and returns ‘true’ if the value is ‘true. Else ‘returns ‘false’ if the value is ‘false’ and throws an exception if the value is any other string than boolean values. We have written a function to return undefined for normal strings.

How to return a Boolean based on the value of string?

It sets the b variable to a boolean based on the value of the string. Take a look at the contents of the parentheses in the second line. It's returning an actual boolean based on the value of the string. If you're not familiar with the three (3) equals signs, that's a check not only for the value but also the type.


3 Answers

Method 1 :

var stringValue = "true";
var boolValue = (/true/i).test(stringValue) //returns true

Method 2 :

var stringValue = "true";
var boolValue = (stringValue =="true");   //returns true

Method 3 :

var stringValue = "true";
var boolValue = JSON.parse(stringValue);   //returns true

Method 4 :

var stringValue = "true";
var boolValue = stringValue.toLowerCase() == 'true'; //returns true

Method 5 :

var stringValue = "true";
var boolValue = getBoolean(stringValue); //returns true
function getBoolean(value){
   switch(value){
        case true:
        case "true":
        case 1:
        case "1":
        case "on":
        case "yes":
            return true;
        default: 
            return false;
    }
}

source: http://codippa.com/how-to-convert-string-to-boolean-javascript/

like image 120
Ayoub k Avatar answered Oct 10 '22 10:10

Ayoub k


I have been trying different values with JSON.parse(value) and it seems to do the work:

// true
Boolean(JSON.parse("true"));
Boolean(JSON.parse("1"));
Boolean(JSON.parse(1));
Boolean(JSON.parse(true));

// false
Boolean(JSON.parse("0")); 
Boolean(JSON.parse(0));
Boolean(JSON.parse("false"));
Boolean(JSON.parse(false));
like image 33
Gonzalo Avatar answered Oct 10 '22 09:10

Gonzalo


In your scenario, converting a string to a boolean can be done via something like someString === 'true' (as was already answered).

However, let me try to address your main issue: dealing with the local storage.

The local storage only supports strings as values; a good way of using it would thus be to always serialise your data as a string before storing it in the storage, and reversing the process when fetching it.

A possibly decent format for serialising your data in is JSON, since it is very easy to deal with in JavaScript.

The following functions could thus be used to interact with local storage, provided that your data can be serialised into JSON.

function setItemInStorage(key, item) {
  localStorage.setItem(key, JSON.stringify(item));
}

function getItemFromStorage(key) {
  return JSON.parse(localStorage.getItem(key));
}

Your example could then be rewritten as:

setItemInStorage('CheckOutPageReload', [this.btnLoginNumOne, this.btnLoginEdit]);

And:

const pageLoadParams = getItemFromStorage('CheckOutPageReload');
if (pageLoadParams) {
  this.btnLoginNumOne = pageLoadParams[0];
  this.btnLoginEdit = pageLoadParams[1];
}
like image 7
nunocastromartins Avatar answered Oct 10 '22 11:10

nunocastromartins