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
}
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
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 });
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)