JavaScript is in array

Let's say I have this:

var blockedTile = new Array("118", "67", "190", "43", "135", "520");

There's more array elements but those are just few for readability purposes. Anyways, I could do a "for" loop but it would do 500 loops everytime you click on the map... is there any other way to see if a certain string is in an array?


Solution 1:

Try this:

if(blockedTile.indexOf("118") != -1)
{  
   // element found
}

Solution 2:

As mentioned before, if your browser supports indexOf(), great! If not, you need to pollyfil it or rely on an utility belt like lodash/underscore.

Just wanted to add this newer ES2016 addition (to keep this question updated):

Array.prototype.includes()

if (blockedTile.includes("118")) {
    // found element
}