There is no implementation of a hash table (hashtable) like C#,java in JavaScript. In JS, the object attribute is the implementation of the hash table, so as long as the object on the sealing method, simple use of OBEJCT management properties of the method can achieve a simple and efficient hashtable.
First, briefly introduce some of the methods for attributes:
Enumeration of attributes:
A for/in loop is a way of traversing an object's properties. Such as
Copy code code as follows:
var obj = {
Name: ' Obj1 ',
AGE:20,
Height: ' 176cm '
}
var str = ';
for (var name in obj)
{
str + = name + ': ' + obj[name] + ' \ n ';
}
alert (str);
Output is: name:obj1
Age:20
height:176cm
Check to see if the property exists:
The in operator can be used to test whether a property exists.
Copy code code as follows:
This.containskey = function (key)
{
Return (key in entry);
}
Delete attribute
Use the delete operator to delete an object's properties. A property that is deleted using the delete for/in will not enumerate the property, and the in operator will not detect the property.
Delete Entry[key];
Delete Obj.name;
The following is the implementation of the hash table (hashtable) JS:
Copy code code as follows:
function HashTable ()
{
var size = 0;
var entry = new Object ();
This.add = function (key, value)
{
if (!this.containskey (key))
{
Size + +;
}
Entry[key] = value;
}
This.getvalue = function (key)
{
return This.containskey (key)? Entry[key]: null;
}
This.remove = function (key)
{
if (This.containskey (key) && (delete entry[key))
{
Size--;
}
}
This.containskey = function (key)
{
Return (key in entry);
}
This.containsvalue = function (value)
{
For (Var prop in entry)
{
if (entry[prop] = = value)
{
return true;
}
}
return false;
}
This.getvalues = function ()
{
var values = new Array ();
For (Var prop in entry)
{
Values.push (Entry[prop]);
}
return values;
}
This.getkeys = function ()
{
var keys = new Array ();
For (Var prop in entry)
{
Keys.push (prop);
}
return keys;
}
This.getsize = function ()
{
return size;
}
This.clear = function ()
{
size = 0;
Entry = new Object ();
}
}
Test:
Code
Copy code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<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");
$ (' #console '). HTML (Map.getkeys (). Join (' | ') + ' <br> ');
})
</script>
<body>
<div id= "Console" ></div>
</body>