7

I have JavaScript code in my app that checks values using the OR(||) condition as in code snippet below. The number of OR conditions is not small in most of my code.

Question: Is there a way to make the code that has many multiple OR conditions more concise by using something like this:value IN ('s','b','g','p','z')? I was interested in something that comes as close as possible to an IN clause.

if(value === "s" || value === "b" || value === "g" || value === "p" || value === "z") {

  //do something

 }
Sunil
  • 20,653
  • 28
  • 112
  • 197

6 Answers6

6

The simplest way is to create an array of valid values, then make sure your value is in that list. You can use the indexOf method for that:

var allowed = ['s', 'b', 'g', 'p', 'z'];
if (allowed.indexOf(value) !== -1) {
  // do something
}

The ES6 standard introduces Sets, which has a has method to do a similar thing on unique values.

This will work for strings, numbers, and other simple non-object values. Comparing objects with === will only succeed if the array/set already contains the exact same object.

ssube
  • 47,010
  • 7
  • 103
  • 140
2

You could do something like this:

var values = ['s','b','g','p','z'];

if (values.indexOf(value) > -1)
Kevin Boucher
  • 16,426
  • 3
  • 48
  • 55
2

You can also use javascript .includes() helper.

The includes() method determines whether an array includes a certain element, returning true or false as appropriate.

var array1 = [1, 2, 3];

console.log(array1.includes(2));
// expected output: true

var pets = ['cat', 'dog', 'bat'];

console.log(pets.includes('cat'));
// expected output: true

console.log(pets.includes('at'));
// expected output: false
Shift 'n Tab
  • 8,808
  • 12
  • 73
  • 117
1

There is, in fact, an in in JavaScript and it can be used for your case exactly. Construct a dictionary with any values you like, but using your allowed letters as the keys:

allowed = { 's':true, 'b':true, 'g':true, 'p':true, 'z':true };

Now you can use the in test directly (and more efficiently than a lookup in a list). Here copied from my JavaScript console:

's' in allowed
true

'q' in allowed
false
Wolf
  • 4,254
  • 1
  • 21
  • 30
1

A solution for single characters:

var value = 'g';

if (~'sbgpz'.indexOf(value)) {
    document.write(value + ' found');
}

A solution for strings characters:

var value = 'go';

if (~['so', 'bo', 'go', 'po', 'zo'].indexOf(value)) {
    document.write(value + ' found');
}

A solution for object properties:

var value = 'go';
var toDo = {
    so: function () { document.write('prop so found'); },
    go: function () { document.write('prop go found'); }
};

value in toDo && toDo[value]();
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Why not lodash's includes function?

var val = 's';
var criterion = ['s','b','g','p','z'];
_.includes(criterion, val);

returns true;

George Stocker
  • 57,289
  • 29
  • 176
  • 237
  • How would this be applied to the code snippet I have? – Sunil Aug 13 '15 at 18:08
  • Can I pass a variable as first parameter to _includes? var v = "'s','b','g','p','z'". Just saw your edit when I posted this. So it seems like a nice library. – Sunil Aug 13 '15 at 18:11
  • 1
    Or just use `criterion.includes(val)` as now included with ES7 or by the question `if( ['s','b','g','p','z'].includes(value) )` – vol7ron Jan 25 '18 at 03:30