Wednesday, 18 September 2013

Javascript if value is in array else in next array

Javascript if value is in array else in next array

I have found a few posts on here with similar questions but not entirely
the same as what I am trying. I am currently using a simple if statement
that checks the data the user enters then checks to see if it starts with
a number of different values. I am doing this with the following:
var value = string;
var value = value.toLowerCase();
country = "NONE";
county = "NONE";
if (value.indexOf('ba1 ') == 0 || value.indexOf('ba2 ') == 0 ||
value.indexOf('ba3 ') == 0) { //CHECK AVON (MAINLAND UK) UK.AVON
country = "UK";
county = "UK.AVON";
}
I have about 20-30 different if, else statements that are basically
checking the post code entered and finding the county associated. However
some of these if statements are incredibly long so I would like to store
the values inside an array and then in the if statement simply check
value.indexOf() for each of the array values.
So in the above example I would have an array as follows for the statement:
var avon = new Array('ba1 ','ba 2','ba3 ');
then inside the indexOf() use each value
Would this be possible with minimal script or am I going to need to make a
function for this to work? I am ideally wanting to keep the array inside
the if statement instead of querying for each array value.
Edit:
just found a post on here mentioning the following JQuery:
$.inArray(value, array) am going to have a play around with this

No comments:

Post a Comment