HashMap plays an irreplaceable role in programming. It provides data storage and reading methods such as m. put (key, value); m. get (key);, which is very convenient. However, such an object is not provided in JavaScript (HTML4.0. The following code is used to create a Map object. I have been using it for many years and it works well for your reference.
1. Map source code
/** Map is a general map object for storing key value pairs
* @ Param m-default set of properties
*/
Var Map = function (m ){
Var map;
If (typeof m = 'undefined') map = new Array ();
Else map = m;
/**
* Get a list of the keys to check
*/
This. keys = function (){
Var _ keys = new Array ();
For (var _ I in map ){
_ Keys. push (_ I );
}
Return _ keys ;//
};
/**
* Put stores the value in the table
* @ Param key the index in the table where the value will be stored
* @ Param value the value to be stored
*/
This. put = function (key, value ){
Map [key] = value;
};
/**
* Return the value stored in the table
* @ Param key the index of the value to retrieve
*/
This. get = function (key ){
Return map [key];
};
/**
* Remove the value from the table
* @ Param key the index of the value to be removed
*/
This. remove = function (key ){
Map [key] = null;
Delete map [key];
};
/**
* Clear the table
*/
This. clear = function (){
Delete map;
Map = new Array ();
};
}
2. Create a Map object
Var m = new Map ();
M. put ("id", "1000 ");
M. put ("name", "James ");
3. Use www.2cto.com
<Div id = "testMap" '> </div>
<Script type = 'text/javascript '>
Document. getElementById ("testMap"). innerHTML = m. get ("name ");
</Script>
From the wj800 Column