I want to identify the array element which matches a string value, eg
<script>
cars = new Array()
cars[0]=”Ford”
cars[1]=”Nissan”
cars[2]=”Ferrari”
cars[3]=”BMW”
</script>
User activity on the page is going to give me a user typed string representing the type of car. Firstly I want to identify whether the string matches any value in my array, which I can do in what seems a bit long winded a way by either using indexOf to look at each array value in turn, or converting the array to a string and looking for a match.
For example, the user types “Mercedes”. I want to be able to identify that “Mercedes” doesn’t appear in the list.
However if the user types in “Ferrari”, after confirming that it appears in the list, I want to return the value “2” representing the index value of the location where the match was found in the array. This is what I can’t find any way of doing, long winded or otherwise.
(I know in this example I could get the user to select from a drop down menu, but this is only a very simplified example of what I want to do – it’s the method that’s important, not the exact application)