In JavaScript we don't have any built-in hashtable. But hashtable is a very useful data structure that we often need. Although we don't have hashtable in JavaScript, it is really very simple to implement a hashtable in JavaScript. In this post, we will how to implement a simple hashtable. Definitely we will not use any looping to search the items. Because, in that case the real purpose of hashtable will not be served. In JavaScript, we can use object and object property to implement hashtable.
First of all let us see the basics of the JavaScript object and object property.
Declaring a JavaScript Object:
There are alternate way of creating a javascript object. The simplest is as follows:
This creates a simple javascript object that has no property. We can also create a javascript object using new.
Creating a Property of an Object:
We can create a property of an existing object as follows:
Here we have 2 properties. We can also have properties that will be a function.
Accessing Javascript object property:
It is very simple to understand that we can access a property of a javascript object by simply using .property_name.
But there is another way of accessing property.
The benefit of using this approach this, we can use variable as the index. So we can access properties dynamically based on variable.
Implementing HashTable:
Now we can easily generate hashtable using the above knowledge.
Creating a HashTable Class:
Using the above way, we will not be able to use multiple hashtable, if we need. So we can create a javascript hashtable class.
We can use the hashtable as follows:
var hashTable_new = new HashTable();
First of all let us see the basics of the JavaScript object and object property.
Declaring a JavaScript Object:
There are alternate way of creating a javascript object. The simplest is as follows:
var hashTable = {};
This creates a simple javascript object that has no property. We can also create a javascript object using new.
var hashTable = new Object();
Creating a Property of an Object:
We can create a property of an existing object as follows:
hashTable.id = 1;
hashTable.name = 'a hashtable';
Here we have 2 properties. We can also have properties that will be a function.
hashTable.SetItem = function(key, value) {
; //implement hashtable set item here based on key
}
hashTable.GetItem = function(key) {
; //implement hashtable get item by key here
}
Accessing Javascript object property:
It is very simple to understand that we can access a property of a javascript object by simply using .property_name.
hashTable.name
But there is another way of accessing property.
hashTable['name']
hashTable['id']
hashTable['SetItem']('akey', 'aValue');
hashTable['GetItem']('akey'));
Implementing HashTable:
Now we can easily generate hashtable using the above knowledge.
var hashTable = {};
hashTable.SetItem = function(key, value) {
hashTable[key] = value;
}
hashTable.GetItem = function(key) {
return hashTable[key];
}
hashTable.SetItem('aKey', 'aValue');
alert(hashTable.GetItem('aKey'));
Creating a HashTable Class:
Using the above way, we will not be able to use multiple hashtable, if we need. So we can create a javascript hashtable class.
function HashTable() {
var hashTableItems = {};
this.SetItem = function(key, value) {
hashTableItems[key] = value;
}
this.GetItem = function(key) {
return hashTableItems[key];
}
}
var hashTable = new HashTable();
hashTable.SetItem('aKey', 'aValue');
alert(hashTable.GetItem('aKey'));
var hashTable_new = new HashTable();
hashTable_new.SetItem('anotherKey', 'anotherValue');
alert(hashTable_new.GetItem('anotherKey'));
No comments:
Post a Comment