Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-08-2012, 11:59 PM   PM User | #1
nachos
New to the CF scene

 
Join Date: Oct 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
nachos is an unknown quantity at this point
finding a substring in an array?

ok so i have it so that ill type a command in like /ban @name and itll
take everything after @ and put it into a variable (substring method).

i also have a users list which takes down everyones user name and user id in the chat room im in and puts it into an array in the format name=userid

so lets say someone is represented in the userlist array as
johnson = 1000000000

if i try to use array.indexOf(substring) it returns -1 every time....

even if substring = johnson

so long story short my question is how would i compare my substring to the users list and have it find their name which is attached to their user id and give me the index location of their name, strip the user id out of it and put it into a different array.
nachos is offline   Reply With Quote
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,187
Thanks: 59
Thanked 3,995 Times in 3,964 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
Old 10-09-2012, 01:01 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 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
I don't understand this part of your post:
Quote:
give me the index location of their name, strip the user id out of it and put it into a different array.
I did the "give me the index location".

But I don't understand the point of putting the result(s) in a different array.

Presumably, there will only be one result, so why does it need to go into an array??

And even if you modified my code to find multiple results, after stripping out the userids all that is left are the numeric values, so the new array would end up looking like
Code:
[ 1000000, 1887331, 2000181 ]
How would that be useful?
__________________
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
Old 10-09-2012, 01:06 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,187
Thanks: 59
Thanked 3,995 Times in 3,964 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
I also have to ask: Suppose your incoming array looks like this:
Code:
[  "adamson = 123", "jones = 775", "adams = 999" ]
If you ask for "adams", you are going to find "admason" first. Is that really what you want?

Seems to me like this kind of array is a mistake. I think you would want an object with multiple properties, thus:
Code:
var userinfo = {
    "adamson" : 123,
    "jones" : 775,
    "adams" : 999
    };
And now you could just do
Code:
    var value = userinfo["adams"];
Much more efficient and you won't get "false positive" matches.
__________________
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
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:21 AM.


Advertisement
Log in to turn off these ads.