简体   繁体   English

如何检查所选数字是否在数组中?

[英]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/ 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.我想我应该以某种方式检查“marker.eres”是否包含所选月份的值,但我不知道如何。所以我想寻求帮助。

I hope I have said it clearly, thank you in advance!我希望我已经说清楚了,提前谢谢你!

Thanks it already works based on the comments!谢谢它已经根据评论工作了!

As pointed out by @GrafiCode , the ereshonap array contained numbers, while eres was a string.正如@GrafiCode所指出的, ereshonap数组包含数字,而eres是一个字符串。 Therefore, I needed to convert the string to an integer using parseInt(eres) before checking if it was in the array.因此,在检查它是否在数组中之前,我需要使用parseInt(eres)将字符串转换为整数。

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

Use the console.log(variable.isArray());使用 console.log(variable.isArray()); if true then its and array else if false then non array如果为真则其和数组否则如果为假则非数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM