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,525
  • 2
  • 18
  • 45
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
  • 66,980
  • 10
  • 72
  • 94
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
  • 5,032
  • 2
  • 29
  • 45
Cayce K
  • 2,288
  • 1
  • 22
  • 35
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,644
  • 11
  • 24