Javascript does not implement a hash table (hashtable) like c # and java. In js, the implementation of object attributes is the hash table. Therefore, as long as the point method is encapsulated on the object, the simple and efficient hashtable can be implemented simply by using obejct to manage attributes.
First, we will briefly introduce some methods for attributes:
Attribute enumeration:
For/in loops are used to traverse object attributes. For example:
Copy to ClipboardReference: [www.bkjia.com] 1 var obj = {
2 name: 'obj1 ',
3 age: 20,
4 height: '20180101'
5}
6
7 var str = '';
8 for (var name in obj)
9 {
10 str + = name + ':' + obj [name] + '\ n ';
11}
12 alert (str );
Output: name: obj1
Age: 20
Height: 176
Check whether the property exists:
The in operator can be used to test whether an attribute exists.
This. containsKey = function (key)
{
Return (key in entry );
}
Delete attributes
Use the delete operator to delete the attributes of an object. If you delete an attribute by using delete, for/in does not enumerate the attribute, and the in operator does not detect the attribute.
1 delete entry [key];
2 delete obj. name;
The following describes how to implement the js of a hash table (hashtable:
Copy to ClipboardReference: [www.bkjia.com] 1 function HashTable ()
2 {
3 var size = 0;
4 var entry = new Object ();
5
6 this. add = function (key, value)
7 {
8 if (! This. containsKey (key ))
9 {
10 size ++;
11}
12 entry [key] = value;
13}
14
15 this. getValue = function (key)
16 {
17 return this. containsKey (key )? Entry [key]: null;
18}
19
20 this. remove = function (key)
21 {
22 if (this. containsKey (key) & (delete entry [key])
23 {
24 size --;
25}
26}
27
28 this. containsKey = function (key)
29 {
30 return (key in entry );
31}
32
33 this. containsValue = function (value)
34 {
35 for (var prop in entry)
36 {
37 if (entry [prop] = value)
38 {
39 return true;
40}
41}
42 return false;
43}
44
45 this. getValues = function ()
46 {
47 var values = new Array ();
48 for (var prop in entry)
49 {
50 values. push (entry [prop]);
51}
52 return values;
53}
54
55 this. getKeys = function ()
56 {
57 var keys = new Array ();
58 for (var prop in entry)
59 {
60 keys. push (prop );
61}
62 return keys;
63}
64
65 this. getSize = function ()
66 {
67 return size;
68}
69
70 this. clear = function ()
71 {
72 size = 0;
73 entry = new Object ();
74}
75}
Test:
Copy to ClipboardReference: [www.bkjia.com] <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> HashTable </title>
<Script type = "text/javascript" src = "/js/jquery. js"> </script>
<Script type = "text/javascript" src = "/js/HashTable. js"> </script>
<Script type = "text/javascript">
Function MyObject (name)
{
This. name = name;
This. toString = function (){
Return this. name;
}
}
$ (Function (){
Var map = new HashTable ();
Map. add ("A", "1 ");
Map. add ("B", "2 ");
Map. add ("A", "5 ");
Map. add ("C", "3 ");
Map. add ("A", "4 ");
Var arrayKey = new Array ("1", "2", "3", "4 ");
Var arrayValue = new Array ("A", "B", "C", "D ");
Map. add (arrayKey, arrayValue );
Var value = map. getValue (arrayKey );
Var object1 = new MyObject ("Small 4 ");
Var object2 = new MyObject ("small 5 ");
Map. add (object1, "Small 4 ");
Map. add (object2, "Small 5 ");
(('{Le'}.html (map. getKeys (). join ('|') + '<br> ');
})
</Script>
</Head>
<Body>
<Div id = "console"> </div>
</Body>
</Html>