如何搜索多个值的字符串?

时间:2014-08-21 05:54:23

标签: javascript

此代码有效:

if ((filenameTmp == "thunderstorm") || 
   (filenameTmp == "fog") || 
   (filenameTmp == "hail") ||
   (filenameTmp == "heavy_snow") ||
   (filenameTmp == "rain") ||
   (filenameTmp == "sleet") ||
   (filenameTmp == "snow"))
{ document.getElementById("twilightBG").style.display = "none"; }

但是我想缩短它,我认为这样可行,但它没有。

if(filenameTmp.indexOf ("thunderstorm", "fog", "hail", "heavy_snow", "haze", "sleet", "snow")> -1)
{ document.getElementById("twilightBG").style.display = "none"; }

如果我只有这样的搜索,它确实有效:

if(filenameTmp.indexOf ("haze")> -1)
{ document.getElementById("twilightBG").style.display = "none"; }

如何搜索多个实例?或者,还有更好的方法?感谢。

8 个答案:

答案 0 :(得分:4)

为您提供三种选择:

  1. 地图对象

  2. 数组查找

  3. switch

  4. 详细说明:

    1. 您可以使用查找地图对象:

      // In a common declarations area
      var weather = {
          "thunderstorm": true,
          "fog": true,
          "hail": true,
          "heavy_snow": true,
          "rain": true,
          "sleet": true,
          "snow": true
      };
      
      // Where you want the check
      if (weather[filenameTmp]) {
          document.getElementById("twilightBG").style.display = "none";
      }
      

      请注意,如果您愿意,可以进行内联:

      if ({ "thunderstorm": true, "fog": true, "hail": true, "heavy_snow": true, "rain": true, "sleet": true, "snow": true }[filenameTmp]) {
          document.getElementById("twilightBG").style.display = "none";
      }
      

      请注意,如果filenameTmp的值为"toString""valueOf"或类似值,则会从中获得误报。如果您使用的是真正启用ES5的引擎,则可以使用构建器函数获取映射(不具有toString等对象): / p>

      function pureMap(props) {
          var o = Object.create(null);
          var key;
          if (props) {
              for (key in props) {
                  o[key] = props[key];
              }
          }
          return o;
      }
      

      然后:

      // In a common declarations area
      var weather = pureMap({
          "thunderstorm": true,
          "fog": true,
          "hail": true,
          "heavy_snow": true,
          "rain": true,
          "sleet": true,
          "snow": true
      });
      
      // Where you want the check
      if (weather[filenameTmp]) {
          document.getElementById("twilightBG").style.display = "none";
      }
      
    2. 或者您可以使用数组,但搜索是线性的,而浏览器可以优化上面的地图查找:

      // In a common declarations area
      var weather = [
          "thunderstorm",
          "fog",
          "hail",
          "heavy_snow",
          "rain",
          "sleet",
          "snow"
      ];
      
      // Where you want the check
      if (weather.indexOf(filenameTmp) !== -1) {
          document.getElementById("twilightBG").style.display = "none";
      }
      

      同样,可以内联:

      if ([ "thunderstorm", "fog", "hail", "heavy_snow", "rain", "sleet", "snow" ].indexOf(filenameTmp) !== -1) {
          document.getElementById("twilightBG").style.display = "none";
      }
      
    3. 还有switch选项:

      switch (filenameTmp) {
          case "thunderstorm":
          case "fog":
          case "hail":
          case "heavy_snow":
          case "rain":
          case "sleet":
          case "snow":
              document.getElementById("twilightBG").style.display = "none";
              break;
      }
      

答案 1 :(得分:3)

您可以使用这样的数组方法:

if (["thunderstorm", "fog", "hail", "heavy_snow", "haze", "sleet", "snow"].indexOf(filenameTmp) >= 0) {

答案 2 :(得分:3)

if (["thunderstorm", "fog", "hail", "heavy_snow", "haze", "sleet", "snow"].indexOf(filenameTmp) !== -1) {
document.getElementById("twilightBG").style.display = "none";
}

答案 3 :(得分:3)

您可以将match()方法与正则表达式

一起使用

var.match(^(?:apple|pear|whatever)$/)

答案 4 :(得分:2)

如果包含方法直接添加到 Array.prototype ,检查会更清晰:

Array.prototype.contains = function(obj) { return this.indexOf(obj) > -1; };

这允许检查:

if (['thunderstorm', 'fog', 'hail', 'heavy_snow', 'rain', 'sleet', 'snow'].contains(filenameTmp)) {
document.getElementById("twilightBG").style.display = "none";

}

答案 5 :(得分:1)

使用Array并遍历每个条目。这是最有效的方法。一旦遇到任何一个条目,它就不会循环通过所有条目。

function containsAny(str, substrings) {
    for (var i = 0; i != substrings.length; i++) {
        var substring = substrings[i];
        if (str == substring) {
            return true;
        }
    }
    return null;
}

var result = containsAny(filenameTmp, ["thunderstrom", "rain"]); // add values you want
if (result) {
    document.getElementById("twilightBG").style.display = "none";
}

希望这有帮助

答案 6 :(得分:1)

对于前沿解决方案,请使用Set。请记住,Opera和Safari目前不支持它:

var weather = new Set();

weather.add("thunderstorm");
weather.add("fog");
weather.add("hail");
weather.add("heavy_snow");
weather.add("rain");
weather.add("sleet");
weather.add("snow");

if (weather.has(filenameTmp)) {
    document.getElementById("twilightBG").style.display = "none";
}

答案 7 :(得分:1)

您可以使用regx:

if (filenameTmp.match(/^(thunderstorm|fog|hail|heavy_snow|rain|sleet|snow)$/)) {
    document.getElementById("twilightBG").style.display = "none";
    }

或js 5的数组:

if (['thunderstorm', 'fog', 'hail', 'heavy_snow','rain','sleet','snow'].indexOf(filenameTmp) >= 0) {
document.getElementById("twilightBG").style.display = "none";
}

或Jquery的inArray方法:

if ($.inArray(filenameTmp, ['thunderstorm', 'fog', 'hail', 'heavy_snow','rain','sleet','snow']) >= 0) {
document.getElementById("twilightBG").style.display = "none";
}