Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript If statement, looking through an array

Mind has gone blank this afternoon and can't for the life of me figure out the right way to do this:

if(i!="3" && i!="4" && i!="5" && i!="6" && i!="7" && i!="8" && i!="9" && i!="2" && i!="19" && i!="18" && i!="60" && i!="61" && i!="50" && i!="49" && i!="79" && i!="78" && i!="81" && i!="82" && i!="80" && i!="70" && i!="90" && i!="91" && i!="92" && i!="93" && i!="94"){

//do stuff

}

All those numbers need to be in an array, then I can check to see if "i" is not equal to any 1 of them.

like image 604
Adam Tomat Avatar asked Aug 06 '10 15:08

Adam Tomat


People also ask

How do you find the value of an array in JavaScript?

The Array.find() method returns the value of the array element that passes a test (provided by a function). The method executes the function once for each element present in the array: If it finds an array element where the function returns a true value, find() returns the value of that array element (and does not check the remaining values)

How to check if an element is present in an array?

If the element found, the flag value will change inside the if condition and that’s how we can check whether it is present or not. Now let’s consider another method of checking if the element is present in the array or not. It's the includes () method, which is now commonly used.

How to return the first element of an array in JavaScript?

So the find () method returns the first element inside an array which satisfies the callback function. The callback function can take in the following parameters: currentItem: This is the element in the array which is currently being iterated over. index: This is the index position of the currentItem inside the array.

How do I iterate through an array of objects in JavaScript?

Well, we can use the find () method to do just that. The find () method is an Array.prototype (aka built-in) method which takes in a callback function and calls that function for every item it iterates over inside of the array it is bound to.


1 Answers

var a = [3,4,5,6,7,8,9];

if ( a.indexOf( 2 ) == -1 ) { 
   // do stuff
}

indexOf returns -1 if the number is not found. It returns something other than -1 if it is found. Change your logic if you want.

Wrap the numbers in quotes if you need strings ( a = ['1','2'] ). I don't know what you're dealing with so I made them numbers.

IE and other obscure/older browsers will need the indexOf method:

if (!Array.prototype.indexOf)  
{  
  Array.prototype.indexOf = function(elt /*, from*/)  
  {  
    var len = this.length >>> 0;  

    var from = Number(arguments[1]) || 0;  
    from = (from < 0)  
         ? Math.ceil(from)  
         : Math.floor(from);  
    if (from < 0)  
      from += len;  

    for (; from < len; from++)  
    {  
      if (from in this &&  
          this[from] === elt)  
        return from;  
    }  
    return -1;  
  };  
}  
like image 162
meder omuraliev Avatar answered Sep 22 '22 06:09

meder omuraliev