DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Hash Table - Playing

01.21.2010
| 3945 views |
  • submit to reddit
        // Code tell all!
//Any question info (a) inov.es

//I see this this simple implementation Hash table
var Hash = function(){
		var indexes = new Array();
		var values	= new Array();
		//objHash.set('indexName', 123)
		this.set = function(name,value){
			var locationIndex = indexes.indexOf(name);
			if(locationIndex==-1){
				locationIndex = indexes.length;
				indexes[locationIndex] = name;
			}
			//values[locationIndex] = value;
			values[indexes.indexOf(name)]=value;
		}
		//objHash.get('indexName')
		this.get = function(name){
			return values[indexes.indexOf(name)];
		}
		
		//objHash.each(function(index,value))
		this.each = function(callback){
			document.write(indexes.length)
 			for(var i=0;i<indexes.length;i++){
				callback(indexes[i], values[i]);
			}
		}
	}
//Usage Example
var names = new Hash();
names.set('steven',2);
document.write("Find " + names.get('steven') + "<br />");

names.each(function(index,value){
	document.write("<li>" + index + " - "+value+"</li>");
}
//This is very fun, but....

//...you can use Javascript Hash native object

var objc = {'steven':10, 'lise':200, 'arthur':300};
objc['maria']=1000;

var ind = 'artur';
objc[ind]=7000;
for (x in objc) {
	document.write(objc[x]+' - '+x+'<br/>');
};