8

Is there a short way to write the following using either JavaScript or jQuery?

if (this.id==="a" || this.id==="b" || this.id==="c" || this.id==="d")
2
  • 5
    Here's a page to compare the performance of various suggestions below: jsperf.com/set-memberbship Commented Jan 31, 2011 at 2:21
  • @Jason LeBrun I have a +1 for the nice demo -- but as [more-often-then-not] always, clarity first. I blame my CPU for the slow FF posting ;-) Commented Jan 31, 2011 at 3:12

5 Answers 5

6

How about this?

if ( this.id in { "a":1, "b":1, "c":1, "d":1 } ) {
  ...
}

... or this?

if("abcd".indexOf(this.id) > -1) {
   ...
}

Sign up to request clarification or add additional context in comments.

5 Comments

+1 I probably wouldn't use 1 as the dummy value, though.
@ClosureCowboy: Thanks for the tip... I also added another example, although it is really not that readable in my opinion. :)
i wouldn't combine all lettere as one word. This will not work if the variable is more then 1 letter, although it is a nice way to write it.
And here I was writing out the array. Silly me.
@Alex: you are right, I strike it out... @Jason, :D ... I give you +1 for throwing the array brackets at me. :)
5
if ( ['a','b','c','d'].indexOf( this.id ) >= 0 ) { ... }

or

if ( this.id in {'a':0,'b':0,'c':0,'d':0} ) { ... }

4 Comments

If this.id is 'a', the first example wouldn't know it.
Both your @limc's amswer (d in o) are similar and fastest way to write it. Thanks for the performance link you provided.
@alex: You'll probably want the in version if you need wide browser support since indexOf doesn't have as much cross-browser support.
In fact you could probably shorten up the in version a bit by getting rid of the quotation marks around the property names in the object. As long as the IDs are valid javascript identifiers, you don't need em.
2

One possibility is a switch statement.

switch(this.id){case"a":case"b":case"c":case"d":
    //do something
}

2 Comments

that works too but we end up repeating the word case 4 times just as in my example repeating the word this.id 4 times.
@alex: Yes it does, but it is a bit shorter, so I thought I'd throw it out there. :o)
1

You can try the following code. Especially when you have more than four test values.

if (/^[abcdef]$/.test(this.id)) {
    ...
}

3 Comments

How about if the variable is more then one letter.
if(/^(val1|val2|val3)$/.test(this.id)) { ... }
Updated the test script to include these two ideas as well.
0

The inline anonymous hash (d in o) performance was misrepresented in the tests as originally written, because the hash wasn't inline in the test.

Oddly enough, the true inline hash case, compared to the predefined hash case, is much slower in Firefox 4, but 50% faster in Chrome 12.

But a more important point is that d in o misses the point of a hash—that you don't have to iterate to find things.

Two lines, but still pretty short, and by far the fastest:

var o = {a:1,b:1,c:1,d:1};
if(o[this.id]){...}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.