Re: Best way to find if item is in array?



"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






.



Relevant Pages