Home > Back-end >  How to check if a selected number is in an array?
How to check if a selected number is in an array?

Time:05-17

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!

CodePudding user response:

Use the console.log(variable.isArray()); if true then its and array else if false then non array

CodePudding user response:

Thanks so it already works based the comments!

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);
            }
            // Categories don't match
            else {
                marker.setVisible(false);
            }
        }
    }
  • Related