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

78
Visualizações
Return array that is not empty

I have two array like this

const array1 = ['1','2','3'];
const array2 = [];

But it can be that array1 is emptyand second array2 have members, what i need is that i want to create getter to return me array that is not empty, something like this

 get notEmptyArray(): string[] {
    const array1 = ['1','2','3'];
    const array2 = [];
    return array1 or array2;
  }

I know i can do it like

if (typeof array !== 'undefined' && array.length === 0) {
    // the array is defined and has no elements
}
if (array && !array.length) {
    // array is defined but has no element
}

But is there some simple inliner for that?

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

0

Using a ternary operator (or two) could make your solution very concise. This will first check whether array1 has members and if so, return it. If not, it will check for array2 and return that or null (if it is also empty):

 return array1 && array1.length ? array1 : array2 && array2.length ? array2 : null
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