如何在字符串中搜索多个值?

Nic*_*oll 5 javascript

这段代码的工作原理:

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

不过我想缩短它,我认为这会起作用,但事实并非如此。

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

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

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

我怎样才能搜索多个实例?或者,还有更好的方法?谢谢。

T.J*_*der 4

三种选择供您选择:

  1. 地图对象

  2. 数组查找

  3. switch

细节:

  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";
    }
    
    Run Code Online (Sandbox Code Playgroud)

    请注意,如果您愿意,您可以内联执行此操作:

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

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

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

    然后:

    // 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";
    }
    
    Run Code Online (Sandbox Code Playgroud)
  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";
    }
    
    Run Code Online (Sandbox Code Playgroud)

    再说一次,这可以是内联的:

    if ([ "thunderstorm", "fog", "hail", "heavy_snow", "rain", "sleet", "snow" ].indexOf(filenameTmp) !== -1) {
        document.getElementById("twilightBG").style.display = "none";
    }
    
    Run Code Online (Sandbox Code Playgroud)
  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;
    }
    
    Run Code Online (Sandbox Code Playgroud)