The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
hash tables in javascript?
Discuss hash tables in javascript? in the JavaScript Development forum on Dev Shed. hash tables in javascript? JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 31st, 2002, 01:17 PM
|
|
matthewdoucette.com
|
|
Join Date: May 2002
Posts: 635

Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 11
|
|
|
hash tables in javascript?
Can you do hash tables in javascript?
__________________
Matthew Doucette / Xona.com
|

August 31st, 2002, 02:29 PM
|
|
Contributing User
|
|
Join Date: Apr 2002
Location: Inside the GNU/Hurd kernel
Posts: 492
Time spent in forums: 2 h 1 m
Reputation Power: 12
|
|
|

August 31st, 2002, 02:39 PM
|
|
matthewdoucette.com
|
|
Join Date: May 2002
Posts: 635

Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 11
|
|
|
Thanks. Now, can you show me how to define multiple hashes all at once instead of one at a time?
|

August 31st, 2002, 03:33 PM
|
|
Contributing User
|
|
Join Date: Apr 2002
Location: Inside the GNU/Hurd kernel
Posts: 492
Time spent in forums: 2 h 1 m
Reputation Power: 12
|
|
Yeah,heres an example:
Code:
var myHashtable = {name: "Someone",age: "2"};
So a list of <key>:<value> delimited by commas,in a block.
|

September 1st, 2002, 10:09 AM
|
|
matthewdoucette.com
|
|
Join Date: May 2002
Posts: 635

Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 11
|
|
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! 
|

September 1st, 2002, 10:29 AM
|
|
matthewdoucette.com
|
|
Join Date: May 2002
Posts: 635

Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 11
|
|
|
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?
|

September 1st, 2002, 09:31 PM
|
|
Senior Citizen
|
|
Join Date: Jan 2001
Location: leftcoast
Posts: 2,019
Time spent in forums: < 1 sec
Reputation Power: 15
|
|
|
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'.
|

September 1st, 2002, 09:50 PM
|
|
matthewdoucette.com
|
|
Join Date: May 2002
Posts: 635

Time spent in forums: 10 h 8 m 16 sec
Reputation Power: 11
|
|
|
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!
|

September 2nd, 2002, 07:03 PM
|
|
Contributing User
|
|
Join Date: Jan 2002
Location: Seattle WA
Posts: 863
  
Time spent in forums: 22 sec
Reputation Power: 13
|
|
|
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.
|

September 3rd, 2002, 05:04 PM
|
|
Senior Citizen
|
|
Join Date: Jan 2001
Location: leftcoast
Posts: 2,019
Time spent in forums: < 1 sec
Reputation Power: 15
|
|
|
<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.
|

September 3rd, 2002, 06:19 PM
|
|
Contributing User
|
|
Join Date: Jan 2002
Location: Seattle WA
Posts: 863
  
Time spent in forums: 22 sec
Reputation Power: 13
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|