SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb HashMap in javascript

    This may be obvious if you are a js guru, but I did a little STFW yesterday but was unable to come up with an implementation of a HashMap for javascript. It turns out that you can code one up fairly easily using the static object syntax:

    Code:
    var targInfo = {
    	Set : function(foo,bar) {this[foo] = bar;},
    	Get : function(foo) {return this[foo];}
    }
    You can then use this object as follows:
    Code:
    targInfo.Set('AT33', Array('N18',5,60));
    foo='AT33';
    alert(targInfo.Get(foo));
    alert(targInfo[foo][1]);
    HTH someone else as well...
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  2. #2
    SitePoint Wizard davidjmedlock's Avatar
    Join Date
    Dec 2002
    Location
    Nashville, TN USA
    Posts
    1,688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very cool. I'm always looking for ways to make JavaScript work more like Java, or more like a fully featured OOP language... It's really quite fun when you get into it...

  3. #3
    SitePoint Member
    Join Date
    Feb 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Hi sweatje,

    The Javascript hashmap is great. However, I have a question. Is it possible to retrieve the key using the value? I.e. is there a way to use Array('N18',5,60) to retrieve AT33?

    Now it works well to use key to retrieve value, if it can be used to use value to retrieve key, that will be great.

    Thank you very much for your help.

    Best regards,
    Sheng


Bookmarks

Posting Permissions

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