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

How to search string for multiple values?

本文关键字:搜索 字符串      更新时间:2023-09-26

代码正常运行:

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

如何搜索多个实例?还是有更好的办法?谢谢。

三个选项:

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

    请注意,如果您愿意,可以内嵌:

    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的引擎,你可以通过使用builder函数获得映射(没有toString之类的对象):

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

    同样,可以是inline:

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

您可以将match()方法与正则表达式一起使用

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

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

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

如果将contains方法添加到Array中,检查将更加清晰。直接:

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";

}

使用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";
}

希望能有所帮助

对于出血边缘解决方案,请使用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";
}

您可以使用regx:

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

:

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