Hashtable object

9th January 2014

The Javascript  specification states that object keys are not sorted and will appear in any order. Although some browsers still present objects in an order in which they were created, when using 'for $key in $obj', it is not a reliable way to get objects by name in an ordered manner. This is where a specific class for hashtables (string-based keys) would prove useful in Javascript:

function HashTable(obj) {
this.obj = obj;
this.arr = [];

if (obj.constructor === {}.constructor) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
this.arr.push({
key: key,
val: obj[key]
});
}
}
} else if (obj.constructor === [].constructor) {
//if an array has been given, then assign keys as the index
for (var i=0; i<obj.length; i++) {
this.arr.push({
key: i,
val: obj[i]
});
}
}
}

HashTable.ascSort = function(a, b) {
if (b>a) return -1;
else if (b<a) return 1;
else return 0;
};

HashTable.descSort = function(a, b) {
return HashTable.ascSort(b, a);
};

//make a hashtable from part of another hashtable
HashTable.fromArray = function(arr) {
var newHashtable = new HashTable({});

newHashtable.arr = arr;
newHashtable.obj = newHashtable.makeObject();

return newHashtable;
};

//make a new object from the array values in the hashtable, (produces this.obj)
HashTable.prototype.makeObject = function() {
var obj = {};

this.each(function(key, value) {
obj[key] = value;
});

return obj;
};

HashTable.prototype.splice = function() {
this.arr.splice.apply(this.arr, this.arguments);
};

HashTable.prototype.sortKeys = function(compFunc) {
if (compFunc===undefined) {
compFunc = HashTable.ascSort;
}

this.arr.sort(function(a, b) {
return compFunc(a.key, b.key);
});
};

HashTable.prototype.sortValues = function(compFunc) {
if (compFunc===undefined) {
compFunc = HashTable.ascSort;
}

this.arr.sort(function(a, b) {
return compFunc(a.val, b.val);
});
};

HashTable.prototype.getKeys = function() {
var keys = [];

this.each(function(key, value) {
keys.push(key);
});

return keys;
};

HashTable.prototype.each = function(func) {
for (var i=0; i<this.arr.length; i++) {
var obj = this.arr[i];

if (func(obj.key, obj.val, i)===false) return false;
}

return true;
};

HashTable.prototype.getValues = function() {
var values = [];

this.each(function(key, value) {
values.push(value);
});

return values;
};

HashTable.prototype.getValue = function(key) {
return this.obj[key];
};

HashTable.prototype.getPair = function(i) {
return this.arr[i];
};

HashTable.prototype.getKeyFromIndex = function(i) {
return this.arr[i].key;
};

HashTable.prototype.getValueFromIndex = function(i) {
return this.arr[i].val;
};

Usage

You can use the hashtable on existing objects to sort them and present them in a sorted object, like a wrapper:

var sortedObj = new HashTable({ a: 1, b: 2, c: 3, d: 4});
sortedObj.sortKeys();

//you can now traverse the sortedObj knowing that the object is sorted by the keys correctly
sortedObj.each(function(key, val, index) {
console.log(key + ": " + val);
});

Make a comment

Contribute to this article and have your say.