Hashtable implementation in Javascript
JavaScript does not have a hash table (hashtable) like C # or Java ),
However, arrays in Javascript only have simple functions similar to 'hash table', as shown below:
- VaR arr = new array ();
- Arr ['item1'] = 'the value of item 1 ';
- Arr ['item2'] = 'the value of item 2 ';
- Alert (ARR ['item1']);
- Alert (ARR ['item2']);
However, the above functions do not meet our actual requirements. In addition, query traversal is not convenient. We need to expand them on the basis of array,
In the following example, we can use arrays in JS to implement hashtable functions,
- Function hashtable (){
- This. Clear = hashtable_clear;
- This. containskey = hashtable_containskey;
- This. containsvalue = hashtable_containsvalue;
- This. Get = hashtable_get;
- This. isempty = hashtable_isempty;
- This. Keys = hashtable_keys;
- This. Put = hashtable_put;
- This. Remove = hashtable_remove;
- This. size = hashtable_size;
- This. tostring = hashtable_tostring;
- This. Values = hashtable_values;
- This. hashtable = new array ();
- }
- Function hashtable_clear (){
- This. hashtable = new array ();
- }
- Function hashtable_containskey (key ){
- VaR exists = false;
- For (var I in this. hashtable ){
- If (I = Key & this. hashtable [I]! = NULL ){
- Exists = true;
- Break;
- }
- }
- Return exists;
- }
- Function hashtable_containsvalue (value ){
- VaR contains = false;
- If (value! = NULL ){
- For (var I in this. hashtable ){
- If (this. hashtable [I] = value ){
- Contains = true;
- Break;
- }
- }
- }
- Return contains;
- }
- Function hashtable_get (key ){
- Return this. hashtable [Key];
- }
- Function hashtable_isempty (){
- Return (this. size = 0 )? True: false;
- }
- Function hashtable_keys (){
- VaR keys = new array ();
- For (var I in this. hashtable ){
- If (this. hashtable [I]! = NULL)
- Keys. Push (I );
- }
- Return keys;
- }
- Function hashtable_put (Key, value ){
- If (Key = NULL | value = NULL ){
- Throw 'nullpointerexception {'+ key +'}, {'+ value + '}';
- } Else {
- This. hashtable [Key] = value;
- }
- }
- Function hashtable_remove (key ){
- VaR RTN = This. hashtable [Key];
- // This. hashtable [Key] = NULL;
- This. Hashtable.Splice(Key, 1 );
- Return RTN;
- }
- Function hashtable_size (){
- VaR size = 0;
- For (var I in this. hashtable ){
- If (this. hashtable [I]! = NULL)
- Size ++;
- }
- Return size;
- }
- Function hashtable_tostring (){
- VaR result = '';
- For (var I in this. hashtable)
- {
- If (this. hashtable [I]! = NULL)
- Result + = '{' + I + '}, {' + this. hashtable [I] + '}/N ';
- }
- Return result;
- }
- Function hashtable_values (){
- VaR values = new array ();
- For (var I in this. hashtable ){
- If (this. hashtable [I]! = NULL)
- Values. Push (this. hashtable [I]);
- }
- Return values;
- }
Hastable class usage:
- // Instantiate a custom hash table class
- VaR hashtable = new hashtable ();
- Hashtable. Put (0, 'abc'); // 0 indicates the key and 'abc' indicates the value.
- Hashtable. Put (1, '123 ');
- Hashtable. Put (2, '88a ');
- Hashtable. Put (3, '88a ');
- // Traverse hashtable, equivalent to foreach in C # and Java
- For (var key in hashtable.Keys() {/* Use the keys Method */
- Alert (hashtable.Get(Key); // traverse value by key
- }
- // Traverse hashtable, equivalent to foreach in C # and Java
- For (var key in hashtable. hashtable) {/* use the hashtable attribute */
- Alert (hashtable.Get(Key); // traverse value by key
- }
- Alert (hashtable.Containskey(1); // return true
- Alert (hashtable.Containskey(4); // if the key is 4 because it does not exist, false is returned.
Alert (hashtable.Containsvalue('20140901'); // return true
- Alert (hashtable.Containsvalue('Doogs'); // returns false because the value of 'mobiogs' does not exist.
- Hashtable.Remove(1); // remove the element whose key is 1
- Alert (hashtable.Containskey(1); // The element whose key is 1 has been removed by the upstream reomve () method, so false is returned.
- // Other hastable methods are easy to use. You can test them by yourself)