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

149
Visualizações
dynamically put condition in if block in Javascript

Is there a way by which we can dynamically put condition in "if" block according to user input?

like:-

const filter
if (location === true) { // location is user input 
  filter = first condition
}
if (skill === true) { // skill is user input 
  filter = second condition
}
if (filter) {
  /* filter contains dynamic value if location is true then filter should contain first condition, if skill is 
  true filter should contain second condition and if both are two true then filter should contain both conditions*/

  console.log("Hello")
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can evaluate the condition inside the if statement, then assign the result of the comparison to filter:

const input = 'a';
var filter
if (input === 'a') {
  filter = 1 == true; //first condition
}else if (input === 'b') {
  filter = 0 == true; //second condition
}
if (filter) {
  console.log("Hello")
}

about 4 years ago · Juan Pablo Isaza Relatório

0

We need to rename location but perhaps you one or more of these?

const filter = []
let loc = true;
let skill = false;
if (loc) filter.push("location") 
if (skill)  filter.push("skill") 
// show

if (filter.length) { // more than 0 items
  filter.forEach(item => console.log(item))
}
if (filter[0] === "skill") console.log("only skill")
else if (filter.length===1 && filter[0] === "location") console.log("only location")
else if (filter.length == 2) console.log("both")

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