I am having an array like this format
var aa = ["a","b","c","d","e","f"];
var categoryid = ["a","x","e","w","q","p"];
And I wants to check this list like this
for(var ii = 0; ii < aa .length; ii++){
iid += " && category != "+aa [ii];
}
Also I need to check condition like this,
if(categoryid != 0 iij) {
alert("value not present");
}
Here this condition is not working like this exactly. Here I need like this format
if(categoryid != 0 && categoryid != 'a') {
alert("value not present");
}
Please help me..
3 Answers 3
You can check if a value is in an array (or, in this case, not in an array) using the array's indexOf method (assuming newer browsers).
Alternatively, try this shim:
function in_array(array,value) {
if( array.indexOf) return array.indexOf(value) > -1;
for( var i=0, l=array.length; i<l; i++) {
if( array[i] == value) return true;
}
return false;
}
Comments
If you're trying to say that you want to test that category.id is not equal to any value within the array then you can use the .indexOf() method:
if (categoryid != 0 && aa.indexOf(category.id) === -1) {
alert("value not present");
}
Note that .indexOf() is not supported in IE8 and older, but MDN explains how to work around that.
1 Comment
Javascript can't be so dynamic, what you need is like below:
if(categoryid != 0 && aa.indexOf(category.id) == -1) {
alert("value not present");
}
4 Comments
eval() in one of its appearances.eval() :)eval(). (Actually I don't agree that eval() is always a bad idea, but I really don't think it's appropriate here.)eval() (but there are some). I just wanted to correct the statement "it is not possible".
category.idis not equal to any value within the array?iijin the conditionif(categoryid != 0 iij)? And also is itcategoryidorcategory.id?