In SQL we can see if a string is in a list like so:
Column IN ('a', 'b', 'c')
What's a good way to do this in javascript? I realize one can use the switch function:
var str = 'a'
var flag = false;
switch (str) {
case 'a':
case 'b':
case 'c':
flag = true;
default:
}
if (thisthing || thatthing || flag === true) {
// do something
}
But this is a horrible mess. It's also clunky to do this:
if (thisthing || thatthing || str === 'a' || str === 'b' || str === 'c') {
// do something
}
And I'm not sure about the performance or clarity of this:
if (thisthing || thatthing || {a:1, b:1, c:1}[str]) {
// do something
}
Any ideas?
UPDATE
I didn't think to mention that in this case, I have to use IE as it's for a corporate intranet page. So ['a', 'b', 'c'].indexOf(str) !== -1
won't work.
Note: here's the full version of the indexOf function for browsers that don't support it. And here's my version (since I don't care about list position or traversing in reverse, this should be faster):
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(item) {
var i = this.length;
while (i--) {
if (this[i] === item) return i;
}
}
}