Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda