Test for value in Javascript Array

Javascript

Javascript Problem Overview


In SQL Server, I could say:

WHERE X IN(1,2)

How would you rewrite the following in JavaScript:

if (X==1 || X==2) {}

Javascript Solutions


Solution 1 - Javascript

Use indexOf to see if x is in an array.

if([1,2].indexOf(x) !== -1)

Solution 2 - Javascript

Using Array .includes method.

if ([1, 2].includes(x)) {
  // array has x
}

Solution 3 - Javascript

Try using an array, and then its .indexOf().

 var myNumbers = [1,2];
 var foo = 4;
 var bar = 1;

 var exists = (myNumbers.indexOf(bar) > -1); //true
 var notExists = (myNumbers.indexOf(foo) > -1); //false

Solution 4 - Javascript

There's no silver bullet. There will be a few gotchas.

If you do indexOf as some answers suggest, you need to remember that Array.indexOf is not supported in all browsers, so you need to provide your own fallback. Also this will have a performance of O(n) since it needs to traverse the whole array, which might not be ideal if you're dealing with a huge array.

If you use the in operator as other answers suggest, you need to remember that in Javascript object's properties are always strings, so don't expect === checks to work if you're checking for numbers.

In this particular example you suggested, I would just go for the good old if (X==1 || X==2).

Solution 5 - Javascript

if (x in {1:true, 2:true}) { }

Or, if you want to abstract it you could do it like this http://snook.ca/archives/javascript/testing_for_a_v

function oc(a) { var o = {}; for(var i=0;i

Still... not the most scalable thing to be doing

Solution 6 - Javascript

Requires Javascript 1.6

if ((new Array(1, 2)).indexOf(X) != -1) {
}

Solution 7 - Javascript

I know we have in_array() function in PHP, but I never heard of a similar function in JS. I think you gotta do it the old way:

function contains(a, obj) {
var i = a.length;
while (i--) {
if (a[i] === obj) {
return true;
}
}
return false;
}

Solution 8 - Javascript

Function to convert the array into an object literal

function oc(a)
{
 var o = {};
 for(var i=0;i<a.length;i++)
 {
  o[a[i]]='';
 }
  return o;
}

You can call the function like

if( name in oc(['Tom', 'Harry','Sue']) ) { ... }

Solution 9 - Javascript

Just fun:

if (X*X+1*2 == (1+2)*X) {}

or self-explaining:

if ((X-1)*(X-2) == 0) {}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhillip SennView Question on Stackoverflow
Solution 1 - JavascriptPeter OlsonView Answer on Stackoverflow
Solution 2 - JavascriptvitkonView Answer on Stackoverflow
Solution 3 - Javascriptp.campbellView Answer on Stackoverflow
Solution 4 - JavascriptadamJLevView Answer on Stackoverflow
Solution 5 - JavascriptTom GrunerView Answer on Stackoverflow
Solution 6 - JavascriptvbenceView Answer on Stackoverflow
Solution 7 - JavascriptMichaelView Answer on Stackoverflow
Solution 8 - JavascriptPiyush MattooView Answer on Stackoverflow
Solution 9 - JavascriptypercubeᵀᴹView Answer on Stackoverflow