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 โ After doing the above, I personally used JS objects as associative arrays that are accessible from the box.
However, I would especially like to mention two methods that actually turned out to be useful for the convenience of using a hash table.
Viz: dict.hasOwnProperty (key) and delete dict [key]
Read this post as a good resource on this implementation / use. Dynamically create keys in an associative JavaScript array
THANKS!
Vaibhav Aug 30 '13 at 14:10 2013-08-30 14:10
source share