Javascripthashtable: code _ javascript skills

Source: Internet
Author: User
Javascript does not have a hash table (hashtable) like c # or java. However, arrays in javascript only have simple functions similar to hash tables. The Code is as follows:


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,

The Code is as follows:


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:

The Code is as follows:


// 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); // traverses 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); // traverses value by key
}
Alert (hashTable. containsKey (1); // return true
Alert (hashTable. containsKey (4); // returns false because key 4 does not exist.
Alert (hashTable. containsValue ('20140901'); // return true
Alert (hashTable. containsValue ('mobidogs '); // if the value of 'mobidogs' does not exist, false is returned.
HashTable. remove (1); // remove the element whose key is 1
Alert (hashTable. containsKey (1); // returns false because the element whose key is 1 has been removed by the upstream reomve () method.
// Other hastable methods are easy to use. You can test them by yourself)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.