0

I have this function:

dosomething(): void {
this.service.check(this.person.name, this.person.name).subscribe(response => {
  this.isGreatPerson = response == null ? false :response;
});
}

this.isGreatPerson should get the response (true or false) and if the response is undefined or null, make this.isGreatPerson false. The solution should be in one line.

Thanks!

3 Answers3

1

Since you literally just want the boolean equivalent of response, convert response to a boolean value:

this.isGreatPerson = Boolean(response);
// or as Drenai wrote:
this.isGreatPerson = !!response;
Ace
  • 1,028
  • 2
  • 10
  • 22
0

If you just use braces, you can achieve a combined check for null and undefined.

doSomething(): void {
     this.service.check(this.person.name, this.person.name).subscribe(response => {
        this.isGreatPerson = (response) ? response : false;
     });
}

If response is nor undefined neither null it will be returned. Otherwise false is the answer.

0

this.isGreatPerson = !!response

!! is the equivalent to converting any value into its boolean representation

If response is true or false it returns true or false, if it's null or undefined it returns false

Drenai
  • 11,315
  • 9
  • 48
  • 82