lotsoftools

Hashtable JavaScript

This snippet displays how to implement and use a hashtable in JavaScript.

function HashTable(obj)

{

this.length = 0;

this.items = {};

for (var p in obj) {

if (obj.hasOwnProperty(p)) {

this.items[p] = obj[p];

this.length++;

}

}

}

This code defines a hashtable in JavaScript using a function. The hashtable is created with the help of the 'items' object.

The 'for' loop iterates through the object passed to the function, checking if the property belongs to the actual object and is not just inherited.

If the property belongs to the object, it's added to the 'items' object and the 'length' property is incremented.