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

205
Visualizações
How to use logical or operator with multiple values?

How to simplify the code when using multiple or operators. I have a list of numbers from 0 to 6 separated by logical or.Is there any way to simplify it?

if (filteredMnth === 'mnth') {
         
  return (new Date(exp?.date).getMonth().toString() === "0" || "1" || "2" || "3" || "4" || "5" || "6"   )

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

0

For this specific case you can do ->

if (filteredMnth === 'mnth') {
  return new Date(exp?.date).getMonth() <= 6;
}

about 4 years ago · Juan Pablo Isaza Relatório

0

A nice pattern for this, is to create an Array of valid or accepted values and use the Array.prototype.includes to check for one from the user input:

const validValues = [ 0, 1, 2 ];
const input = 2;

validValues.includes(input);
// => true

const input2 = 3;

validValues.includes(input2);
//=> false

As @Robby Cornelissen already mentioned in the comment, in your case it really does not make any sense, but I'm including this pattern here to answer a more generic version of your question.

about 4 years ago · Juan Pablo Isaza Relatório

0

Since you are having multiple values you can make use of List and includes method as below

const validMonths = ["1", "2", ...]
const monthToCheck = new Date(exp?.date).getMonth().toString()
if(validMonths.includes(monthToCheck)){
    //Evaluates true if value exist
}
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