Copy Code code 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 '));
But the above function, does not conform to our actual request, another query traversal is not convenient, we need to expand on the basis of array,
Below we can use the array in JS to achieve similar Hashtable function,
Copy Code code 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;
}
How to use HasTable class:
Copy Code code as follows:
//instantiate a custom hash table class
var hashTable = new hashTable ();
Hashtable.put (0, ' abc ');//0 is key, ' abc ' is Value
Hashtable.put (1, ' 123 ');
Hashtable.put (2, ' 88a ');
Hashtable.put (3, ' 88a ');
//Traverse hashTable, equivalent to the foreach in C # and Java
for (var key in Hashtable.keys ()) {/* Use the Keys method/
alert (hashtable.get ke y)); Press key to traverse value
}
///traverse hashTable, equivalent to the foreach
for (var key in hashtable.hashtable) in C # and Java) {/* with hashTable Sex */
Alert (Hashtable.get (key));//By Key traverse value
}
Alert (Hashtable.containskey (1));//return True
Alert (ha Shtable.containskey (4)); Returns false
Alert (hashtable.containsvalue (' 888 ')) because there is no key 4;//return True
Alert (Hashtable.containsvalue (' Mobidogs ')); Returns false
Hashtable.remove (1) If there is no value of ' mobidogs ', and//removes the element with key 1
Alert (Hashtable.containskey (1)); Because the element with key 1 has been removed by the uplink reomve () method, return False
//other methods for hastable are simple to use and readers can test themselves (this slightly)