8

right now I have:

if (breadCrumbArr[x] !== 'NEBC' && breadCrumbArr[x] !== 'station:|slot:' &&  breadCrumbArr[x] !== 'slot:' &&  breadCrumbArr[x] !== 'believe') {
    // more code
}

But I think this could be done better...

sherlock
  • 744
  • 7
  • 21

3 Answers3

13

Make an array and use indexOf:

['NEBC', 'station:|slot:', 'slot:', 'believe'].indexOf(breadCrumbArr[x]) === -1
Blender
  • 289,723
  • 53
  • 439
  • 496
  • 2
    +1 - and ignore IE 8-. – JimmiTh Aug 05 '13 at 20:56
  • Alternately, you could use JQuery's `$.inArray(string_name, array_name)`, if that's an option. – talemyn Aug 05 '13 at 20:57
  • 1
    @JimmiTh Don't need to ignore it. Just [include a polyfill](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf#Compatibility). – Jonathan Lonowski Aug 05 '13 at 20:58
  • 1
    wow 8 upvotes in like 2 minutes – sherlock Aug 05 '13 at 20:58
  • I should point out that `.indexOf()` does not give the same result as `!==` so this is not functionally equivalent to the OP's original code. – jfriend00 Aug 05 '13 at 21:03
  • @jfriend00 Can you give an example? – Ian Aug 05 '13 at 21:07
  • @jfriend00 - in what way doesn't it? `.indexOf()` performs the comparison using strict equality, same as `===`, and in this case it will stop checking on the first match - same way as a chained `!==` (although in other cases, such as if the compared values were results of function calls, it obviously would evaluate all, rather than short circuit). – JimmiTh Aug 05 '13 at 21:08
  • @jfriend00 thats chill, i would probably always use != instead of !== if people didn't think it was odd. – sherlock Aug 05 '13 at 21:08
7

You could use a switch statement:

switch(inputString){
  case "ignoreme1":
  case "ignoreme2":
  case "ignoreme3":
    break;
  default: 
    //Do your stuff
    break;
}
Xynariz
  • 1,232
  • 1
  • 11
  • 28
  • 3
    +1 for readability of this solution – Jonathan M Aug 05 '13 at 20:58
  • @JonathanM how about -1 for the lack of flexibility, as values are hardcoded? – Christophe Aug 05 '13 at 23:42
  • @JonathanM I agree that the hardcoded values is bad.. but that was the approach taken by the question, hence that's how I addressed it. – Xynariz Aug 05 '13 at 23:46
  • Sorry for the bad tag - the last comment was meant for @Christophe – Xynariz Aug 06 '13 at 01:47
  • @Christophe, I'm trying to understand what you mean. Can you give an example that doesn't involve hardcoding values? – Jonathan M Aug 07 '13 at 15:59
  • 1
    @JonathanM see the other answers. If you store the values in an array or an object, then you can write code that adds/removes/updates the values. If you have the values in a case statement you cannot change them at runtime. (and for the record I was not serious about the downvote!) – Christophe Aug 07 '13 at 21:44
0

In addition to Blender's answer: if you want to go cross browser, you could also use an object instead of an array:

var words = {
    'NEBC': true, 
    'station:|slot:': true, 
    'slot:': true, 
    'believe': true
};

if (!words[breadCrumbArr[x]]){
    //do stuff
}

It's also faster but it's also definitly uglier since you have to assign a value (in this case true) to every string that is used as property name.

basilikum
  • 10,378
  • 5
  • 45
  • 58