Javascript - Determine if String Is In List

Posted by Emtucifor on Stack Overflow See other posts from Stack Overflow or by Emtucifor
Published on 2010-03-12T01:53:45Z Indexed on 2010/03/12 1:57 UTC
Read the original article Hit count: 258

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about string