Home >Web Front-end >JS Tutorial >Implement your own Map object in JavaScript

Implement your own Map object in JavaScript

高洛峰
高洛峰Original
2016-11-25 14:12:421348browse

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn