Using jQuery, I am trying to check if a value x exists in my array.
Basically, when clicking the testButton I have a var x that starts from zero Using this value I am trying to check if in the array there is an element with value 0. If there is, increment x and perform the array scan again. This should happen until x has a value which is not found in the array. Then at this point I know that I can use this id for the element.
At the moment I have this code that checks if value x is in array, but this happens only for the length of the array.
Next iteration should be: x=3, x is not in array so can be used as id.
Can someone help to implement this. Thanks
var array = ["0","1","2"];
var x = 0;
$("#testButton").click(function(){
$.each(array, function(index,value){
if($.inArray(x.toString(),array == -1)){
console.log('found item with x value in array, increment x and scan the array again');
}
else{
console.log('not in array, add id to element and push current x value to array.');
}
x=x+1;
});
});
inArrayshould beif($.inArray(x.toString(),array) >= 0){["0","1","2"];vs a possible use of[0,1,2];