Here is a neat implementation of Hastable in JS. http://www.mojavelinux.com/articles/javascript_hashes.html
He argues that the approach: “In JavaScript, all non-scalar objects behave as associative arrays, mapping from property keys to values. Keys and values can be scalars, objects, or functions.”
This link is also available in other related articles that may help. Like here .
Edit →
Created a simple dictionary in JS here:
function JSdict() { this.Keys = []; this.Values = []; } // Check if dictionary extensions aren't implemented yet. // Returns value of a key if (!JSdict.prototype.getVal) { JSdict.prototype.getVal = function (key) { if (key == null) { return "Key cannot be null"; } for (var i = 0; i < this.Keys.length; i++) { if (this.Keys[i] == key) { return this.Values[i]; } } return "Key not found!"; } } // Check if dictionary extensions aren't implemented yet. // Updates value of a key if (!JSdict.prototype.update) { JSdict.prototype.update = function (key, val) { if (key == null || val == null) { return "Key or Value cannot be null"; } // Verify dict integrity before each operation if (keysLength != valsLength) { return "Dictionary inconsistent. Keys length don't match values!"; } var keysLength = this.Keys.length; var valsLength = this.Values.length; var flag = false; for (var i = 0; i < keysLength; i++) { if (this.Keys[i] == key) { this.Values[i] = val; flag = true; break; } } if (!flag) { return "Key does not exist"; } } } // Check if dictionary extensions aren't implemented yet. // Adds a unique key value pair if (!JSdict.prototype.add) { JSdict.prototype.add = function (key, val) { // Allow only strings or numbers as keys if (typeof (key) == "number" || typeof (key) == "string") { if (key == null || val == null) { return "Key or Value cannot be null"; } if (keysLength != valsLength) { return "Dictionary inconsistent. Keys length don't match values!"; } var keysLength = this.Keys.length; var valsLength = this.Values.length; for (var i = 0; i < keysLength; i++) { if (this.Keys[i] == key) { return "Duplicate keys not allowed!"; } } this.Keys.push(key); this.Values.push(val); } else { return "Only number or string can be key!"; } } } // Check if dictionary extensions aren't implemented yet. // Removes a key value pair if (!JSdict.prototype.remove) { JSdict.prototype.remove = function (key) { if (key == null) { return "Key cannot be null"; } if (keysLength != valsLength) { return "Dictionary inconsistent. Keys length don't match values!"; } var keysLength = this.Keys.length; var valsLength = this.Values.length; var flag = false; for (var i = 0; i < keysLength; i++) { if (this.Keys[i] == key) { this.Keys.shift(key); this.Values.shift(this.Values[i]); flag = true; break; } } if (!flag) { return "Key does not exist"; } } }
The above implementation can now be used to simulate a dictionary like:
var dict = new JSdict(); dict.add(1, "one") dict.add(1, "one more") "Duplicate keys not allowed!" dict.getVal(1) "one" dict.update(1, "onne") dict.getVal(1) "onne" dict.remove(1) dict.getVal(1) "Key not found!"
This is just basic modeling. It can be further optimized by implementing a more efficient runtime algorithm for working at least in O (nlogn) time complexity or even less. Like merge / quick sort on arrays, and then some B-lookup to search. I have not tried or searched for the mapping of the hash function in JS.
In addition, the key and value for the JSdict obj object can be turned into private variables to be mean.
Hope this helps!
Edit → Good news, now it's a simpler, native and more efficient way to emulate a dict in JS: Also use that JS is weakly typed. Rather, enter the output. Anyway..
Here's how (excerpt from the Google Chrome console):
var myDict = {}; myDict.one = 1; 1 myDict.two = 2; 2 if (myDict.hasOwnProperty('three')) { console.log(myDict.two); } else { console.log('Key does not exist!'); } Key does not exist! VM361:8 if (myDict.hasOwnProperty('two')) { console.log(myDict.two); } else { console.log('Key does not exist!'); } 2 VM362:4 Object.keys(myDict); ["one", "two"] delete(myDict.two); true myDict.hasOwnProperty('two'); false myDict.two undefined myDict.one 1