Sunil
Sunil

Reputation: 21406

IN clause using JavaScript code

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

 }

Upvotes: 7

Views: 8499

Answers (6)

Shift 'n Tab
Shift 'n Tab

Reputation: 9443

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

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386654

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]();

Upvotes: 1

Wolf
Wolf

Reputation: 4452

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

Upvotes: 1

George Stocker
George Stocker

Reputation: 57877

Why not lodash's includes function?

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

returns true;

Upvotes: 0

Kevin Boucher
Kevin Boucher

Reputation: 16675

You could do something like this:

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

if (values.indexOf(value) > -1)

Upvotes: 2

ssube
ssube

Reputation: 48277

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.

Upvotes: 6

Related Questions