21

Possible Duplicate:
array.contains(obj) in JavaScript

Something like:

if (mystring == "a" || mystring == "b" || mystring =="c")

I was hopping to do:

if (mystring in ("a", "b", "c"))

is it possible?

Community
  • 1
  • 1
Diego
  • 34,802
  • 21
  • 91
  • 134
  • 3
    Your example is wrong, you are assigning and not comparing. – epascarello Oct 08 '12 at 12:57
  • Im not talking about arrays @DanielA.White, I know I can add the strings to one, but I believe there are ways to do it without – Diego Oct 08 '12 at 13:12
  • `var strings = [ 'js', 'java', 'css', 'html' ]; var search = 'java'; var checkindex = -1; if ( strings.indexOf( search ) > checkindex) { console.log('available at position : '+strings.indexOf( search )); } else { console.log('not available') }` (or) `var search = 'java'; switch(search) { case 'js': case 'java': case 'html': console.log('available'); break; default: console.log('not - available'); break; } ` – Yash Aug 11 '15 at 13:43

9 Answers9

24

You could use indexOf() like this

if ( [ 'a', 'b', 'c' ].indexOf( mystring ) > -1 ) { ... }

EDIT With ES7 comes a little simplification. As of today just Chrome 47+ and FF 43+ seem to support it:

if ( [ 'a', 'b', 'c' ].includes( mystring ) ) { ... }
Sirko
  • 72,589
  • 19
  • 149
  • 183
5

Using indexOf is first thing that comes to mind, however you have to keep in mind, that there's no .indexOf function in older IEs (so you would have to use your custom code to simulate it, or just go straight to something like jQuery.inArray or underscore.js indexOf).

if ([ 'a', 'b', 'c' ].indexOf( mystring ) > -1 ) { ... }

Side note: as you can see by looking at inArray definition in jQuery source, writing your own indexOf replacement is pretty easy. So like I said - write your own method, copy-paste it from other libraries or just use those libs if you want to be able to use indexOf in every browser out there.

WTK
  • 16,583
  • 6
  • 35
  • 45
4

You could do it the old way

a = "a";
b = ["a","b","c","d"];


function check(a,b){
    i = 0;        
    for (i=0;i<b.length;i++) {
        if (a === b[i]) {
            return true;
        }
    }
    return false;
}

alert(check (a,b))

note that indexOf is a recent addition to the ECMA-262 standard; as such it may not be present in all browsers If you're going to use this with IE it will only work with version 9 or above

Jacob George
  • 2,559
  • 1
  • 16
  • 28
1

You can use indexOf

[1,2,3].indexOf(1) will give 0
[1,2,3].indexOf(4) will give -1

So, you can check if indexOf returns -1.

Similar for strings.

Prasanth
  • 5,230
  • 2
  • 29
  • 61
1

I think something like this:

if (["a", "b", "c"].indexOf(mystring) != -1) {
    // do something
}
Martin
  • 15,820
  • 4
  • 47
  • 56
1
if (["a", "b", "c"].indexOf(mystring) != -1) {}

This would be the best way, however it may not work on some browsers.

dragonfire
  • 407
  • 7
  • 16
0

Your version is the fastest one for three chars, and the most readable.

With a little more possible values, you could use something like this :

var c;
if (s.length==1 && (c=s.charAt(0))>='a' && c<='c') {

But that's mainly for fun...

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

Addtionally: you could create a String extension for the comparison:

String.prototype.in = function(){
  return  ~[].slice.call(arguments).indexOf(this.valueOf()) ? true : false;
}
//usage
var myStr = 'a';
myStr.in('a','b','c'); //=> true
'd'.in('a','b','c');   //=> false

For older browsers: this MDN page contains a shim for Array.indexOf

KooiInc
  • 119,216
  • 31
  • 141
  • 177
-1

Something like

foreach (str in strings) { 
    if (mystring.contains(str)) { 
        ... 
    } 
}

may work.

akluth
  • 8,393
  • 5
  • 38
  • 42