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

119
Visualizações
Verify data in `for` loop

I have an array with strings and I have a string - snowy for example. The task is to verify if the snowy is in arr, if yes, push it into one array, if no, push it into another. I am not sure that use of if(true) is correct way.

const arr = ["sunny", "rainy", "cloudy", "foggy"];

if (true) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === "snowy") {
      someArr.push("snowy");
      // some other conditions
    }
  }
  anotherArr.push("snowy");
  // some other conditions
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can do something like

    const arr = ["sunny", "rainy", "cloudy", "foggy" ]

    if(arr.includes("snowy")){
        someArr.push("snowy");
    }else{
        anotherArr.push("snowy");
    }

In this case you are not going to need the for loop because the inludes() function looks for the string in the complete array. Here you have some tutorial of how to use the includes() function. https://www.w3schools.com/jsref/jsref_includes_array.asp

about 4 years ago · Juan Pablo Isaza Relatório

0

Try with some

const arr = ["sunny", "rainy", "cloudy", "foggy"];

const someArray = [];
const anotherArray = [];

if (arr.some((item) => item === "snowy")) {
  someArray.push("snowy");
} else {
  anotherArray.push("snowy");
}

console.log({ someArray, anotherArray });

about 4 years ago · Juan Pablo Isaza Relatório

0

const arr = ["sunny", "rainy", "cloudy", "foggy"];
const set = new Set(arr);
const someArr = []
const anotherArr = []
  
 set.has(data = "sunny") ? someArr.push(data) : anotherArr.push(data)
 
 console.log(someArr)
 
 console.log(anotherArr)
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