Home » Guides Advanced Article

JavaScript Implementation of Hashtable

2.7/5.0 (3 votes total)
Rate:

Uzi Refaeli
May 03, 2007


Uzi Refaeli
Uzi Refaeli is the CTO of Comet Information Systems which specialize with technology entrepreneurship and web development.
Uzi Refaeli has written 1 articles for JavaScriptSearch.
View all articles by Uzi Refaeli...

Very often there is a need to use a data structure that keeps parameters in a form of key-value. Associative Array (i.e. JavaScript Array Object)is such a solution.
Although it has low memory signature and quick data fetching we cannot iterate thru the array elements using direct key-value interface.

Web applications developed for IE based browsers have the option to use the Microsoft Dictionary object.
Massive use of the object may cause high memory signature and low application performance.

This article introduces a possible solution to this problem, a solution which is deployed in real world running applications.

JavaScript Hashtable Object

The idea behind using this object is having the properties of associative Array bundled with low order computational complexity data fetching,
by adding new methods and capabilities.

Let's start by defining the class members:



Hashtable.prototype.hash = null;

Hashtable.prototype.keys = null;

Hashtable.prototype.location = null;





We have defined 3 member variables:


  • hash - An associative Array that holds the data.

  • keys - A standard Array that holds the Array keys. We will use it when we need to iterate on our list of values.

  • location - Points at the current value.



After defining the class member variables we define the class constructor:


function Hashtable(){

this.hash = new Array();

this.keys = new Array();



this.location = 0;

}



Clear and simple. We initialize our two arrays and our pointer.

After finishing with the definitions part we can start coding...

The first two function that we implement are get and put.
These are the main functions for data storage and retrieval.


Hashtable.prototype.get = function (key)

return this.hash[key];

}



Hashtable.prototype.put = function (key, value){

if (value == null)

return null;



if (this.hash[key] == null)

this.keys[this.keys.length] = key;



this.hash[key] = value;

}



The get function simply returns the value from the Array by the supplied key, while put is required for a little more work.
It initially checks whether the value equals to null (if so the process stops). Then the function checks whether this key is already defined.
If so the value is replaced with the new argument supplied.
Otherwise, a new key is generated and the value is stored accordingly.

Another set of functions let us iterate thru the Array. Instead of introducing the code here we will show how we can use it:


//declare an instance

var items = new Hashtable();



//add 3 values to the hash

items.put("key1", "value1");

items.put("key2", "value2");

items.put("key3", "value3");



//just show that all works well

alert(items.get("key2"));



//start iterating the hash

//Just to be on the safe side (maybe someone has used it before?)

items.moveFirst();

while (tems.next()){ //While we have more elements

//print the key and the value

alert (items.getKey() + " = " + items.getValue());

}



The JavaScript Hashtable Object has some more interesting methods:

  • add - Merges two Hashtables

  • toString - Returns a textual representation of the Object (mainly for debugging)

  • getKeyOfValue - Returns a list of keys that hold a certain value


Add commentAdd comment (Comments: 0)  

Advertisement

Partners

Related Resources

Other Resources

arrow