Reputation: 349
I'm retreiving some information from a server, and I'm getting let's say settings.notificationOn = "T" when the notifications are on, and settings.notificationOn = "F". I want to save it into a variable(notify: boolean) that is boolean. And I want to use this variable in an ion-toggle, that will be checked true when notify is true and not checked when notify is false. How can I do this?
Thanks
Upvotes: 0
Views: 259
Reputation: 2916
To be sure that notificationOn
has not any bad values you can do it like this:
const notify: boolean = this.getNotificationStatus(settings);
getNotificationStatus(settings: { notificationOn: 'T' | 'F' }): boolean {
if (settings.notificationOn === "T") return true;
if (settings.notificationOn === "F") return false;
throw new Error("/* Your error here */");
}
Upvotes: 1
Reputation: 111
if "T" then true, else false.
const notify: boolean = settings.notificationOn === "T" ? true : false;
Upvotes: 0
Reputation: 1315
Simply do:
const notify: boolean = (settings.notificationOn === "T");
Upvotes: 1
Reputation: 2987
You can simply do:
notify: boolean = settings.notificationOn == "T" ? true : false ;
Upvotes: 1
Reputation: 2248
If it's a string that has either 'F' or 'T' then you can use the following code:
const notify = settings.notificationOn === 'T';
Short explanation:
You are assigning to the notify variable the result from the expression of whether settings.notificationOn is equal to 'T'. If it is, value will be true, else, false.
Upvotes: 0