Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,032
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Probably nothing, but like I said - I'm totally new to JavaScript. What would that do?

  • #4
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,032
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    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.

  • #6
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    worth a shot... thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •