Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
In ngIf need to return a boolean from object value

In ngIf need to return a boolean from object value.

var a = {"c" : "b","f": "g"}

a["c"] => 'b'

(a && a["c"]) => 'b' // why here it is not returning true

a && a["c"] => 'b' // How to return a boolean from a value here

!!( a && a["c"]) => true // is it correct way to return true or any other better way available?

<button title="Submit" *ngIf="!!(a && a["c"])">Submit</button>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

"why here it is not returning true"

(a && a["c"]) returns a if a is falsy and a["c"] if a is truthy. An object is always truthy, therefore (a && a["c"]) returns "b".

"How to return a boolean from a value here"

!! is a common operation to convert a truthy value to true and a falsy value to false.

"is it correct way to return true or any other better way available?"

It is correct and I would use !!(a && a["c"]) to convert (a && a["c"]) to boolean.

You can make it shorter with !!a?.c (optional chaining):

<button title="Submit" *ngIf="!!a?.c">Submit</button>

or even

<button title="Submit" *ngIf="a?.c">Submit</button>

if your TypeScript configuration allows it.

I recommend to check the list of truthy and falsy values. Sometimes the results can be very surprising.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!