PDA

View Full Version : Searching value's in array, return index?


Kirl
04-07-2007, 01:21 PM
I remember reading something about a searchByValue (valueAt()?) command but I can't find anything about it, so I may as well've been dreaming...

Is there anything like a function that returns the index of the element(s) that contain the specifed string? I want to search on value instead of index.

Thanks

rwedge
04-07-2007, 04:54 PM
Here's one way<script type="text/javascript">
var test = new Array('one','two','three','four','five','three');
for(var i=0;i<test.length;i++) {
if (test[i] == 'three'){ alert(i); }
}
</script>

Kirl
04-07-2007, 06:38 PM
Thanks, I thought I read about a command that would return the index of the specified key, but thinking back I probably misunderstood it's function (I didn't test it at the time).

Anyway, I solved it like you suggested.

rwedge
04-07-2007, 07:40 PM
You are probably referring to methods for handling strings

david_kw
04-07-2007, 07:41 PM
You might be thinking of Array.indexOf() which only works in FireFox.

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:indexOf

There is also a String.indexOf() which I believe is a standard.

david_kw