Re: Best way to find if item is in array?
- From: "Jim Davis" <newsmonkey@xxxxxxxxxxx>
- Date: Wed, 31 Aug 2005 13:42:36 -0400
"McKirahan" <News@xxxxxxxxxxxxx> wrote in message
news:OIWdnZ2dnZ1bMUOQnZ2dnZ5ciN6dnZ2dRVn-z52dnZ0@xxxxxxxxxxxxxx
<snip>
> Change
> alert(myArray.join.indexOf("jane"));
> to
> alert(myArray.join().indexOf("jane"));
>
> To ensure that your search string is not embedded try this:
>
> var myJoin = "|" + myArray.join("|") + "|";
> alert(myJoin.indexOf("|frank|"));
> alert(myJoin.indexOf("|jane|"));
>
> This presumes that "|" is not a character in your array.
In general, unless your array is very large or you search it a lot, a
client-side loop is just so damn fast why bother playing games?
And if it IS very large or you search it a lot why not set up a hash table?
Then it's just "if ( myHash["Jane"] ) {};" Instant results with no fuss.
Just my opinion of course - which doesn't change the fact that McKirahan's
solution will, indeed, work just fine. ;^)
Jim Davis
.
- Follow-Ups:
- Prev by Date: Re: Interfacing Javascript with CGI
- Next by Date: Re: Can Javascript count letter frequency?
- Previous by thread: Re: Best way to find if item is in array?
- Next by thread: Re: Best way to find if item is in array?
- Index(es):
Relevant Pages
|