IN clause using JavaScript code
Asked Answered
H

6

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

 }
Holomorphic answered 13/8, 2015 at 18:6 Comment(2)
Put the values in an array and use .indexOf()Herbal
possible duplicate of array.contains(obj) in JavaScriptGlandule
M
0

Why not lodash's includes function?

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

returns true;

Mauriac answered 13/8, 2015 at 18:7 Comment(3)
How would this be applied to the code snippet I have?Holomorphic
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.Holomorphic
Or just use criterion.includes(val) as now included with ES7 or by the question if( ['s','b','g','p','z'].includes(value) )Kala
A
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.

Akanke answered 13/8, 2015 at 18:7 Comment(1)
Does indexOf use strict comparision as in ====?Holomorphic
K
2

You could do something like this:

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

if (values.indexOf(value) > -1)
Kenwood answered 13/8, 2015 at 18:8 Comment(0)
L
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
Lepidosiren answered 25/1, 2018 at 3:26 Comment(0)
B
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
Bollard answered 13/8, 2015 at 18:13 Comment(0)
H
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]();
Horsewoman answered 13/8, 2015 at 18:25 Comment(0)
M
0

Why not lodash's includes function?

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

returns true;

Mauriac answered 13/8, 2015 at 18:7 Comment(3)
How would this be applied to the code snippet I have?Holomorphic
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.Holomorphic
Or just use criterion.includes(val) as now included with ES7 or by the question if( ['s','b','g','p','z'].includes(value) )Kala

© 2022 - 2024 — McMap. All rights reserved.