1

Im trying to check an array for a value as such. not sure how to go about. If anyone could help that would be great.

var array1 = [0];

if (DONT KNOW WHAT TO SAY TO CHECK FOR 0 IN THE ARRAY) {
    Do something
}

5 Answers5

0

simple like this

if (array1[0] == "0") {
    //Do something
}

and if you have more values in your array you should use indexOf()

var array1 = [0, 3, 1, 4];
if (array1.indexOf(0) != -1)  {
    //Do something
}
elreeda
  • 4,357
  • 2
  • 17
  • 44
0

DONT KNOW WHAT TO SAY TO CHECK FOR 0 IN THE ARRAY

If you want to check for 0 in the array1 (anywhere in the array), then use indexOf

if (array1.indexOf(0) != -1) 
{
    Do something
}
gurvinder372
  • 64,240
  • 8
  • 67
  • 88
0

You need to access the array. If you have no idea what place the array is you need to go through the array.

for (var i = 0; i < array.length; i++) {
    if (array[i] == 0) {
       //Do all the things
    }
}
Joshua
  • 4,324
  • 2
  • 27
  • 43
Cayce K
  • 2,188
  • 1
  • 20
  • 32
0
if(array1.indexOf(0) == 0){
   //Do your code
}
gambler
  • 67
  • 2
  • 7
0

Also you can do

for(var i in array){
    if(array[i] == 0){
       //Do all the things
    }
}
Khaled Awad
  • 1,444
  • 10
  • 24