/    Sign up×
Community /Pin to ProfileBookmark

Identifying index value of an array item

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)

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@requestcodeApr 20.2004 — You could use a for loop to bump through the array comparing what was entered to the array entry. Like this:

<script>

cars = new Array()

cars[0]="Ford"

cars[1]="Nissan"

cars[2]="Ferrari"

cars[3]="BMW"

function Findcar(elmval)

{

len=cars.length // get number of array entries

which_index=-1

car_match=""

for(i=0;i<len;i++) // bump through array and find match

{

if(elmval==cars[i])

{

which_index=i

car_match=cars[i]

}

}

if(car_match=="")

{alert("Not found")

else

{alert("Car Found and the index is "+which_index)

}

</script>





You would have to pass the value entered to the function which you could do like this:

<form name="myform">

<input type="text" name="mycar" size="10">

<input type="button" value="Click" onClick="Findcar(document.myform.name.value)">

</form>



There are many different ways to do this. The above is just one way.
Copy linkTweet thisAlerts:
@bherringtonauthorApr 20.2004 — Thanks, it's a simple concept, basically running through the array values in turn adding 1 to i until you find a match, then reading the value of i - should have thought of that one myself. Does that mean there's no way to reference the index number of the array value itself directly?
Copy linkTweet thisAlerts:
@requestcodeApr 20.2004 — Not that I know of, but someone else may know of a way.
Copy linkTweet thisAlerts:
@scottlepichNov 21.2006 — <script type="text/javascript">

// first extend the native Array object with a getIndex method

Array.prototype.getIndex = function(v) {

var i = this.length-1;

do {

if (this[i] == v) { return i; }

} while (i--);

}



// <Array>.getIndex( <value> );

// should now return the index of the last occurance of the value in the array

// or undefined if the value is not in the array

myArray = [ 88, 'foo', 42, 'bar', 'bat', 97, 42 ];

alert( myArray.getIndex( 'bat' ) );

alert( myArray.getIndex( 42 ) );

</script>
×

Success!

Help @bherrington spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...