View Single Post
Old 10-09-2012, 12:58 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
The problem is that Array.indexOf and String.indexOf are very very different.

String.indexOf indeed looks for a substring in the string.
But Array.indexOf looks for an array *ELEMENT* (the ENTIRE element) that matches the given test value.

So the quick answer to your question is: Write code!

Code:
function findSubstringInArray( ary, str )
{
    for ( var a = 0; a < ary.length; ++a )
    {
        if ( ary[a].indexOf(str) >= 0 ) { return a; } /* return element number of found match */
    }
    return -1; /* not found */
}
Now... If this is something you do all the time, you might want to extend the Array prototype to add a method that will do as you wish. But if it's something you only do in one place in one page, just use simple code as shown.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote