5

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 this 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 does work if I only have a single search like this:

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

How can I do it searching for multiple instances? Or is there a better way? Thanks.

1

8 Answers 8

4

Three options for you:

  1. A map object

  2. An array lookup

  3. switch

Details:

  1. You can use a lookup 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 that inline 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 has the value "toString", "valueOf", or similar, you'll get false-positives from that. If you're using a true ES5-enabled engine, you can get pure maps (objects that don't have toString and such) by 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";
    }
    
  2. Or you could use an array, but the search is linear whereas browsers can optimize the map lookup 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";
    }
    

    And again, that can be inline:

    if ([ "thunderstorm", "fog", "hail", "heavy_snow", "rain", "sleet", "snow" ].indexOf(filenameTmp) !== -1) {
        document.getElementById("twilightBG").style.display = "none";
    }
    
  3. There's also the switch option:

    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
  • all right, but if (weather[filenameTmp]) is not the same of IndexOf > -1 Aug 21, 2014 at 6:02
4

You can use the match() method together with a regex

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

4
  • +1, but you'll want anchors on that, and if you're not using the resulting match array it's better to use test: if (/^(?:thunderstorm|fog|hail|heavy_snow|rain|sleet|snow)$/.test(filenameTmp)) { /*...*/ } Aug 21, 2014 at 6:28
  • won't your regex check for a string that IS "thunderstorm", "fog" etc.? If I understood it correctly the question asks to check if the string contains those words. (btw feel free to improve the answer if you wish so!)
    – Luca B.
    Aug 21, 2014 at 7:03
  • The OP's code in the question under "This code works:" is checking that the string is "thunderstorm," etc., not that it contains it. Aug 21, 2014 at 7:04
  • My bad, reading the questions from mobile is really a pain. (Fixing the regex right now)
    – Luca B.
    Aug 21, 2014 at 7:05
3

You may use the array approach like this:

if (["thunderstorm", "fog", "hail", "heavy_snow", "haze", "sleet", "snow"].indexOf(filenameTmp) >= 0) {
2
  • This won't work in older browsers (ie < 8 to name one)
    – Luca B.
    Aug 21, 2014 at 6:02
  • Thanks, I did try an array but I got the syntax wrong, this works perfect for my needs (iPhone only). This is the method I'm now using.
    – Nicoll
    Aug 21, 2014 at 6:10
3
if (["thunderstorm", "fog", "hail", "heavy_snow", "haze", "sleet", "snow"].indexOf(filenameTmp) !== -1) {
document.getElementById("twilightBG").style.display = "none";
}
0
2

The check would be cleaner if the contains method were added to Array.prototype directly:

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

This allows the check to be:

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

}

1

Use Array and loop through every entry . this is most efficient way .By this it ll not loop thru all entry once it encounter any one.

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 this helps

1

For a bleeding edge solution use Set. Keep in mind it's not currently supported on 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

You can use regx:

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

or array of js 5:

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

or inArray method of Jquery:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.