Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在字符串中搜索多个值?_Javascript - Fatal编程技术网

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

Javascript 如何在字符串中搜索多个值?,javascript,Javascript,此代码适用于: if ((filenameTmp == "thunderstorm") || (filenameTmp == "fog") || (filenameTmp == "hail") || (filenameTmp == "heavy_snow") || (filenameTmp == "rain") || (filenameTmp == "sleet") || (filenameTmp == "snow")) { document.getEle

此代码适用于:

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"; }
if (["thunderstorm", "fog", "hail", "heavy_snow", "haze", "sleet", "snow"].indexOf(filenameTmp) >= 0) {
如何搜索多个实例?还是有更好的办法?谢谢。

您有三个选择:

  • 地图对象

  • 数组查找

  • 开关

  • 详情:

  • 可以使用查找贴图对象:

    // 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
    等的对象):

    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 = [
        "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";
    }
    
  • 还有
    开关
    选项:

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

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

    if(filenameTmp.indexOf ("haze")> -1)
    { document.getElementById("twilightBG").style.display = "none"; }
    
    if (["thunderstorm", "fog", "hail", "heavy_snow", "haze", "sleet", "snow"].indexOf(filenameTmp) >= 0) {
    
    可以将match()方法与正则表达式一起使用


    var.match(^(?:apple | pear | whatever)$/)
    使用数组并循环遍历每个条目。这是最有效的方法。这样,一旦遇到任何一个条目,它就不会循环通过所有条目

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

    希望这对使用尖端解决方案有所帮助。请记住,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";
        }
    
    或JS5的数组:

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

    如果将contains方法添加到数组中,检查会更干净。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";
    

    }

    你也在使用Jquery,还是只使用纯javascript?好吧,但是如果(weather[filenameTmp])与IndexOf>-1不一样,这在旧浏览器中不起作用(例如<8),谢谢,我确实尝试了一个数组,但语法错了,这非常适合我的需要(仅限iPhone)。这是我现在使用的方法。+1,但是你需要锚定它,如果你不使用生成的匹配数组,最好使用
    test
    如果(/^(?:雷雨|雾|雹|大雪|雨|雨|雪)$/。test(filenameTmp)){/*…*/}
    你的正则表达式不会检查“雷雨”、“雾”等字符串吗。?如果我理解正确,问题会询问检查字符串是否包含这些单词。(顺便说一句,如果你愿意,可以随时改进答案!)“此代码有效:”下问题中的OP代码是检查字符串是否为“雷雨”等,而不是它是否包含该字符串。我不好,从手机上阅读问题真的很痛苦。(正在修复正则表达式)