#1
  1. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    13

    hash tables in javascript?


    Can you do hash tables in javascript?
    Matthew Doucette / Xona.com
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    Inside the GNU/Hurd kernel
    Posts
    492
    Rep Power
    13
    Yes,read the Objects section at http://www.crockford.com/javascript/survey.html

    Comments on this post

    • Matthew Doucette agrees
  4. #3
  5. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    13
    Thanks. Now, can you show me how to define multiple hashes all at once instead of one at a time?
    Matthew Doucette / Xona.com
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    Inside the GNU/Hurd kernel
    Posts
    492
    Rep Power
    13
    Yeah,heres an example:
    Code:
    var myHashtable = {name: "Someone",age: "2"};
    So a list of <key>:<value> delimited by commas,in a block.
  8. #5
  9. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    13
    I should have guessed that but I figured I would ask anyway. It's hard to tell when you have proper formatting, even when it works, with client-side languages.

    Thanks!
    Matthew Doucette / Xona.com
  10. #6
  11. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    13
    One more thing... is there any way to calculate the length of the hash table instead of going through each entry in a loop and incrementing a counter? Also, once you figure out the length, is there anyway to grab a random hash entry? For example, if the hash is 100 entries in lenght and I want to grab #25, can I do this?
    Matthew Doucette / Xona.com
  12. #7
  13. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    Using a for/in loop on a hash returns the entries in the order in which they were defined; so...

    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript" language="javascript">

    var hashObj = Object;
    hashObj['1'] = 'hash1';
    hashObj['2'] = 'hash2';
    hashObj['3'] = 'hash3';
    hashObj['4'] = 'hash4';
    hashObj['5'] = 'hash5';

    function gethashObj(which) {
    var count = 1;
    for (key in hashObj) if (count++ == which) return hashObj[key];
    }

    function go() {
    x=Math.ceil(Math.random()*5);
    alert('random number = ' + x);
    alert('hashObj = ' + gethashObj(x));
    }

    </script>
    </head>
    <body>
    <a href="#" onclick="go()">go</a>
    </body>
    </html>

    As for the other...JS hashes are unordered, unlike arrays, so additional coding is needed (I believe) to get 'length'.
  14. #8
  15. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    13
    Ok. Without being able to find the length 'easily' I am just going to use 2 arrays instead of 1 hash table. I think that is the easiest and most compact method. Thanks for the help!
    Matthew Doucette / Xona.com
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    Yup, you're going to need to use two arrays.

    If you want to go all out, best create a class with the arrays as memebers, that way you can add functions and handlers for your hash tables and create an all-inclusive map class that will keep an internal count of everything. That's on my list of things to do a project of mine.
  18. #10
  19. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript" language="javascript">

    Object.prototype.getLength = function() {
    len = 0;
    for (prop in this) if (this[prop].constructor != Function) ++len;
    return len;
    }

    Object.prototype.output = function (isHTML) {
    var hStr = '';
    for (prop in window) if (window[prop] == this) hStr += 'Obj: ' + prop + '\n';
    for (prop in this) if (this[prop].constructor != Function)
    hStr += prop + ' : ' + this[prop] + ((isHTML) ? '<br>' : '\n');
    return hStr;
    }
    ///////////////////////
    var myHash = {
    prop1: 'thingy' ,
    prop2: 'anotherthingy' ,
    prop3: 'yetanotherthingy' ,
    prop4: 'non-thingy'
    }
    ///////////////////////

    </script>
    </head>
    <body text="darkred"><pre>
    var myHash = {
    prop1: 'thingy' ,
    prop2: 'anotherthingy' ,
    prop3: 'yetanotherthingy' ,
    prop4: 'non-thingy'
    }
    </pre><br><br>

    <a href="#"
    onclick="if(myHash)alert(myHash.getLength())">myHash.getLength()</a><br><br>
    <a href="#"
    onclick="if(myHash)alert(myHash.output())">myHash.output()</a><br><br>
    <a href="#"
    onclick="if(myHash)this.innerHTML=myHash.output(true)">myHash.output(true)</a>
    </body>
    </html>
    Last edited by adios; September 3rd, 2002 at 05:32 PM.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    Damn, adios, your example finally made me notice something. I just caught on that if you assign an array element like myHash['foo']='bar', you can actually get back what the key is. I thought the 'bar' string was hashed, translated to an index, and lost forever after the assignment. Suddenly, an Array object is almost a true hash table. All that I need to do is generate a simple function for searching, which can be done by extending the Array object with a prototype.

    It couldn't get much easier.

IMN logo majestic logo threadwatch logo seochat tools logo