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

127
Visualizações
How to shows nothing if a list of value is undefined in Javascript?

Sorry for this, maybe is an easy and basic question

I'm getting results from an endpoint but some of these values could be empty, then on my screen shows "undefined"

How can I check in a clean code if some of the value is undefined then show nothing?

myvalues() {
  if (!this.myendpointdata) {
    return '';
  }
  const { value1, value2, value3, value4 } =
    this.myendpoindata.master.values;
  return `${value1} ${value2} ${value3} ${value4}`;
}

This is my code. And, for example, value2 shows undefined

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can set empty strings if your values are undefined

myvalues() {
  if (!this.myendpointdata) {
    return '';
  }
  const { value1, value2, value3, value4 } =
    this.myendpoindata.master.values;
  return `${value1 || ""} ${value2 || ""} ${value2 || ""} ${value4 || ""}`;
}

You also can loop through values and filter not undefined values out for display

myvalues() {
  if (!this.myendpointdata) {
    return '';
  }
  const { value1, value2, value3, value4 } = this.myendpoindata.master.values;
  const values = Object.values({ value1, value2, value3, value4 });
  return values.filter(value => !value).join(" ");
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use ternary operations:

return `${value1 ? value1 : ''} ${value2 ? value2 : ''} ${value3 ? value3 : ''} ${value4 ? value4 : ''}`;
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