'How to check if a selected number is in an array?
I want to place markers on a google map with filtering. If the filter only needs to check a single value, my code will work fine. However, if I want to check if the value returned by the filter is in that array, then unfortunately the code doesn't work.
Here is my code:
https://jsfiddle.net/7o8w2eaq/
So the code snippet I would like help with:
filterMarkersEres = function (eres) {
for (i = 0; i < gmarkers1.length; i++) {
marker = gmarkers1[i];
var ereshonap = marker.eres;
let igaze = ereshonap.includes(eres);
//ereshonap.includes(eres);
console.log(igaze);
// If is same category or category not picked
if (ereshonap.includes(eres) || eres.length === 0) {
//
marker.setVisible(true);
}
// Categories don't match
else {
marker.setVisible(false);
}
}
};
So I want to make the ripening time filter work for several months. I think I should somehow check if "marker.eres" contains the value of the selected month, but I can't figure out how.So I want to ask for help with this.
I hope I have said it clearly, thank you in advance!
Solution 1:[1]
Thanks it already works based on the comments!
As pointed out by @GrafiCode, the ereshonap array contained numbers, while eres was a string. Therefore, I needed to convert the string to an integer using parseInt(eres) before checking if it was in the array.
filterMarkersEres = function (eres) {
for (i = 0; i < gmarkers1.length; i++) {
marker = gmarkers1[i];
var ereshonap = marker.eres;
// If is same category or category not picked
if (ereshonap.includes(parseInt(eres)) || eres.length === 0) {
marker.setVisible(true);
} else { // Categories don't match
marker.setVisible(false);
}
}
}
Solution 2:[2]
Use the console.log(variable.isArray()); if true then its and array else if false then non array
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Roman Mahotskyi |
| Solution 2 | Mohammad Maaz |
