snippettypescriptangularCritical
How to convert string to boolean in typescript Angular 4
Viewed 0 times
angulartypescripthowconvertbooleanstring
Problem
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
here
mirror.component.ts
in this component
I tried the solutions in stackoverflow but nothing is worked.
Can anyone help me to fix this .
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 .
Solution
Method 1 :
Method 2 :
Method 3 :
Method 4 :
Method 5 :
source: http://codippa.com/how-to-convert-string-to-boolean-javascript/
var stringValue = "true";
var boolValue = (/true/i).test(stringValue) //returns trueMethod 2 :
var stringValue = "true";
var boolValue = (stringValue =="true"); //returns trueMethod 3 :
var stringValue = "true";
var boolValue = JSON.parse(stringValue); //returns trueMethod 4 :
var stringValue = "true";
var boolValue = stringValue.toLowerCase() == 'true'; //returns trueMethod 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/
Code Snippets
var stringValue = "true";
var boolValue = (/true/i).test(stringValue) //returns truevar stringValue = "true";
var boolValue = (stringValue =="true"); //returns truevar stringValue = "true";
var boolValue = JSON.parse(stringValue); //returns truevar stringValue = "true";
var boolValue = stringValue.toLowerCase() == 'true'; //returns truevar 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;
}
}Context
Stack Overflow Q#52017809, score: 304
Revisions (0)
No revisions yet.