2

Possible Duplicate:
How to nest OR statements in JavaScript?

Is there a way to do this:

 if( variable1 == (variable2 || variable3 || ...) )

I need to check if variable1 is equal to either variable2 variable3 4 5...

I can't directly do this:

 if( (variable1 == variable2) || (variable1 == variable3) || ...) )

because I don't know exactly how many variable2 3 4 5... I have

btw variable2 3 4 5... are the elements of my array. I tried this but nothings happen.

if( variable1 == (variable2 || variable3 || ...) )

update: here's the snippet

let say strskill equal to:

abc|def|ghi|jkl|

.

 var myskl = document.getElementById('strskill').value.split("|");


 for(var q=0; q<(myskl.length); q++)
        {
            var r = r + "myskl[q], ";
            var s = r + "myskl[q]";             
        }

        if(s.indexOf(myArray[i]) > -1)
        {   
            continue;
        }   

I tried your suggestion, but still not working!

Community
  • 1
  • 1
Sam San
  • 6,567
  • 8
  • 33
  • 51

7 Answers7

7

You can't do that because A || B || C || ... always return the first true value.

You could do like below:

if ([variable2, variable3, variable4, ...].indexOf(variable1) > -1) {

}
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • 1
    @IvorySantos [_indexOf is a recent addition to the ECMA-262 standard; as such it may not be present in all browsers_](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf) If you're going to use this with IE it will only work with version 9 or above – Andreas Oct 08 '12 at 08:56
  • @xdazz I tried what your example, please see my updated question. It still not working – Sam San Oct 08 '12 at 09:03
  • @Andreas I tried indexOf and I'm using firefox, but still not working – Sam San Oct 08 '12 at 09:04
  • @IvorySantos your code is seriously broken - you need the _values_ of `myskl[q]`, but you're just creating a string that says `"myskl[q]"` – Alnitak Oct 08 '12 at 09:06
  • @IvorySantos Just check this simple demo. http://jsfiddle.net/7hvmN/ – xdazz Oct 08 '12 at 09:07
1

You have different options.

  • In pure javascript, you could use array.indexOf(variable1) which returns -1 if it hasn't been found in the array. More info here. Be careful of older browsers which would not support this function.
  • lots of different javascript frameworks give this possibility : check this answer on SO with lots of different references

Your code would then be (with the first solution) :

myArray = [variable2, variable3 ...];    
if(myArray.indexOf(variable1) !== -1) 
Community
  • 1
  • 1
Rytek
  • 563
  • 8
  • 22
1

You could also do it this way

a = 1;
b = [1,2,3,4];


function check(a,b){
    i = 0;        
     for (i=0;i<b.length;i++) {
        if (a === b[i]) {
             return true;
        }
    }
    return false;
 }

alert(check (a,b))

Jacob George
  • 2,559
  • 1
  • 16
  • 28
  • I already guess and tried it lately. but I got a problem on how can I "continue;" to the next iteration since I need to escape two loops. Till I saw your answer and let me to think again. And boom, I found a solution. Thanks @kidmenot – Sam San Oct 08 '12 at 09:34
  • glad to be of help @IvorySantos – Jacob George Oct 08 '12 at 09:44
0

this wont' work as what you are doing check if any of variable 2 to n can be considered as the boolean value True and then compare it with the value of varaible1

if varaible 2 to n are in an array you shoud create a fonction is_member taking the array and varaible1 and sending back a bool

this can be done with a for going through the array and returning true if eather value matches varaible1 in the other cases you should return false after the for boucle

Amxx
  • 3,020
  • 2
  • 24
  • 45
0

You cannot do this logical operation directly. Either you have to use indexOf as @xdazz has shown or use a loop as shown below

function inArray(array, val){
    for(var i = 0; i < array.length; i++){
       if(array[i] == val){
          return true;
       }
    }
    return false;
}

var arr = [3, 4, 5, 6, 7, 8, 9];
var var1 = 90;
inArray(arr, var1);
Diode
  • 24,570
  • 8
  • 40
  • 51
0

You will have to use a for / while loop to check this. If you want to keep the if statement intact, you can use an anonymous function: var myArr=[false,true,true,false];

if((function(arr,compareTo){
    for( var i=0;i<arr.length;i++){
     if(arr[i]==compareTo) return true;   
    }
    return false;
})(myArr,true)){
// ⇑⇑⇑⇑     ⇖The variable you want to check against
//The array with the values you want to check
    document.write('Yes!')

}else{
        document.write('No :-(');
}

View this example at JSFiddle

user2428118
  • 7,935
  • 4
  • 45
  • 72
0

Assuming from your update that you want to find if the first entry is found in the rest of the list:

var myskl = document.getElementById('strskill').value.split("|");
var s = myskl.shift();
var found = (myskl.indexOf(s) >= 0);

If the string ss to be found comes from somewhere else, remove the .shift() line.

Note that indexOf() is a recent addition to JS - use a "shim" function to add it to your browser if it doesn't exist - see here for more.

Alnitak
  • 334,560
  • 70
  • 407
  • 495