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

171
Visualizações
nodejs [] operator, avoid picking first char of array of strings

I have an array of strings that sometimes is an array that only contains one string. when that is the case array[0] does not return the whole string but returns the first char in the string.

I can get around it with some if checking if length is > 1 but it is awful.

is there a safe [] operator or method that always return the whole string in position 0 of the array instead of the first char in the string?

This is an express input-based array in an html form. I don't really have too much control over how it's generated.

i can't use a foreach as I have other parallel arrays from that form.

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

0

Before operating your var as an array, you should verify that it is an array.

In you case, i guess that when you get only a string the provider of your data return a string type and when there is more than one string it return an array of string.

So you need to verify the type returned by your data provider and then process with the right type.

if ( Array.isArray(yourVar) ) {
  console.log('My var is an array');
} else {
  console.log('My var is not an array');
}

You can also use typeof to check your var type, for example verify that your var is a string.

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