Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!