Home >Web Front-end >JS Tutorial >Implement your own Map object in JavaScript
HashMap plays an irreplaceable and important role in programming. It provides data storage and reading methods such as m.put(key,value); m.get(key);, which is very convenient. But in JavaScript (HTML4.0 version), there is no such object provided. The following code is used to create a Map object. I have used it for many years with good results. It is for reference by friends in need.
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();
map = m; ();
for (var _i in map){
_keys.push(_i);
}
return _keys;//
(key,value) {
map[key] = value;
;
/**
* Get a list of the keys to check
*/
This.clear = function() {
Delete map;
map = new Array();
};
}
2. Create Map object
var m=new Map();
m.put("id","1000");
m.put("name","Zhang San");
3. Use www.2cto.com
document.getElementById("testMap").innerHTML=m.get("name");