0

My question is, how can I make the program do something if some variable is equal to any element in an array? Is there any other simpler way than just looping through the entire array?

For example: Suppose there is a variable, line = 0; and an array, characters = [1,2,3,4];

So, what I want to do is that, if the variable "line", changes to any of the four elements in the array "characters" (i.e. 1,2,3,4), then the program needs to do something specific.

(The array can be way bigger than that and the elements in the array might be created during the program not beforehand.)

scorpio98
  • 103
  • 1
  • 2
  • 6
  • Why you dont want to use a for-loop? – laaposto Jul 08 '14 at 15:06
  • No. Looping is the way to go. Do it untill you are having performance problems with it. Then come back and ask another question. – Bergi Jul 08 '14 at 15:06
  • Why don't you want to loop through the array? – Stevo Perisic Jul 08 '14 at 15:06
  • Ok, thanks, I will just use the for-loop then. :) – scorpio98 Jul 08 '14 at 15:08
  • 2
    Gentlemen, it looks like we have a bad case of `Premature Optimization` – domokun Jul 08 '14 at 15:09
  • 2
    `Is there any other simpler way than just looping through the entire array?` you want to know if an item is included in the array. If you don't compare it to every values, you can't be sure the value is not in the array. Unless your array is sorted, in which case you can use a binary search. – njzk2 Jul 08 '14 at 15:11
  • Note that [`indexOf` seems to be _slower_ than looping](http://stackoverflow.com/questions/6682951/why-is-looping-through-an-array-so-much-faster-than-javascripts-native-indexof). [JSperf](http://jsperf.com/js-for-loop-vs-array-indexof/193). – Andy Jul 08 '14 at 15:22

4 Answers4

0
if (characters.indexOf(line) >= 0) {
   // It's in the array
}
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
0

Use Array.indexOf to see if an element in in an array. If it returns -1 then the element is not in the array.

var line = 0;
var chars = [1,2,3,4]

if (chars.indexOf(line) === -1) {
  console.log("Not there.");
} else {
  console.log("Array contains " + "'" + line + "'");

}
Mark Feltner
  • 2,041
  • 1
  • 12
  • 23
0

It's not very clear exactly what you are trying to do, but one idea would be to use an object rather than an array so you can do the look up faster. If your "something specific" is a function, you could even have an object where that "something specific" is a function in your object. For example:

var characters = {
    1: function() { alert("hey there"); },
    2: function() { return 1 + 1; },
    3: function() { destroyTheWorld(); }
};

Then you could do something like this:

line = 1;
characters[line]();
Matt Burland
  • 44,552
  • 18
  • 99
  • 171
0

Use indexOf() to search inside an array

http://www.w3schools.com/jsref/jsref_indexof_array.asp

it will return the position of the object inside the array, so it means that is there, just need a comprobation for that maybe like this:

if(position = characters.indexOf(line)){
    //value contains the searched item...
    value = characters[position];
}else{
    //not there
}
Omarchh
  • 1
  • 2