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 04-22-2010, 12:19 AM   PM User | #1
funnyguppy
New to the CF scene

 
Join Date: Apr 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
funnyguppy is an unknown quantity at this point
Look up value in multidimensional array

I'm new to Javascript, and am having a bit of trouble figuring out some things... so hopefully this will be an easy one.

I want a lookup table that matches url fragments to the name I want to show. For instance, if a url contains "yahoo.com", I want to print "Yahoo." For now, I created a multidimensional array that maps url fragments to a name. such as,

Code:
var urlMapping = [
{ urlpath: "yahoo.com", mapto: "Yahoo"}, 
{ urlpath: "google.com", mapto: "Google"}
];
So, say I have a variable urlVar, where the value is some URL (for example, "http://us.mg201.mail.yahoo.com").

Is there any way - other than looping through the array for every variable - to check if urlVar contains any of the substrings in urlMapping.urlPath, and if so, output urlMapping.mapto? (In this case, I want urlVar to find that it contains the substring "yahoo.com" and output "Yahoo".) In my code, I anticipate that I have 100 variables checked against 50 mappings, and performance is important to me.

Thanks in advance!
funnyguppy is offline   Reply With Quote
Old 04-22-2010, 12:37 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
Ummm...what's wrong with simply:
Code:
var mapping = {
    "yahoo.com" : "Yahoo",
    "msn.com" : "MicroSlopNews",
    "google.com" : "gOOgle",
    ...
    };

var value = mapping[ key.toLowerCase() ];
...
??
__________________
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 04-22-2010, 12:44 AM   PM User | #3
funnyguppy
New to the CF scene

 
Join Date: Apr 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
funnyguppy is an unknown quantity at this point
Probably nothing, but like I said - I'm totally new to JavaScript. What would that do?
funnyguppy is offline   Reply With Quote
Old 04-22-2010, 12:49 AM   PM User | #4
funnyguppy
New to the CF scene

 
Join Date: Apr 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
funnyguppy is an unknown quantity at this point
Oh, I think I understand... in the above code, you'd assume that "key" = "yahoo.com", which would return the value of "Yahoo." Which is almost what I need - except that my key would actually be something like "www.yahoo.com/abc/xyz/index.html" - where it's not going to be an exact match to what's in the mapping, but I need to know if the URL contains something in the map.

Any advice for that?
funnyguppy is offline   Reply With Quote
Old 04-22-2010, 01:29 AM   PM User | #5
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
oh, ugh...

No, I don't see a way to do that other than a loop.

Can't even think of a sneaky way to do it with regexp's, though there might be...hmmm...likely not to be that much faster, though.
__________________
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 04-22-2010, 03:49 AM   PM User | #6
funnyguppy
New to the CF scene

 
Join Date: Apr 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
funnyguppy is an unknown quantity at this point
worth a shot... thanks!
funnyguppy 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 03:51 PM.


Advertisement
Log in to turn off these ads.