Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
141
rated 0 times [  146] [ 5]  / answers: 1 / hits: 56219  / 12 Years ago, thu, november 22, 2012, 12:00:00

I have a variable:



var code = de;


And I have an array:



var countryList = [de,fr,it,es];


Could someone help me as I need to check to see if the variable is inside the countryList array - my attempt is here:



    if (code instanceof countryList) {
alert('value is Array!');
}

else {
alert('Not an array');
}


but I get the following error in console.log when it's run:




TypeError: invalid 'instanceof' operand countryList



More From » arrays

 Answers
9

jQuery has a utility function to find whether an element exist in array or not



$.inArray(value, array)


It returns index of the value in array and -1 if value is not present in array. so your code can be like this



if( $.inArray(code, countryList) != -1){
alert('value is Array!');
} else {
alert('Not an array');
}

[#81863] Tuesday, November 20, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mattieparisp

Total Points: 612
Total Questions: 109
Total Answers: 104

Location: Trinidad and Tobago
Member since Fri, May 8, 2020
4 Years ago
;