How do I search a string for multiple values?

This code works:

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

      

However, I would like to shorten it, I thought it would work, but it doesn't.

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

      

It works if I only have one search:

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

      

How can I search for multiple instances? Or is there a better way? Thank.

+3


source to share


8 answers


Three options for you:

  • Map object

  • Search for an array

  • switch



More details:

  • You can use the search map object:

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

    Note that you can do this if you like:

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

    Note that if filenameTmp

    it matters "toString"

    , "valueOf"

    or the like, you will get false positives from this. If you are using a real ES5 backed engine, you can get clean maps (objects that don't have toString

    , etc.) using a builder function:

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

    Then:

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

  • Or you can use an array, but the search is linear, whereas browsers can optimize the map search above:

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

    Again, this can be inline:

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

  • There is also an option 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;
    }
    
          

+4


source


The check will be cleaner if the contains method has been added to Array.prototype directly:

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

      

This allows you to check:



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

      

}

+4


source


You can use an array approach like this:

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

      

+3


source


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

      

+3


source


You can use the match () method along with regex

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

+3


source


Use Array and skip every entry. this is the most efficient way. This will not go through the entire recording after she meets someone.

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

      

Hope it helps

+1


source


Use Set to solve an edge solution . Please be aware that it is currently not supported by Opera and 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";
}

      

+1


source


You can use regx:

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

      

or an array from js 5:

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

      

or inArray of JQuery method:

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

      

+1


source







All Articles