Hashmap is a very commonly used and widely used data type. This article introduces you to the relevant knowledge of js hashMap through example code. The specific code content is as follows:
/*** MAP object, implement MAP function* * Interface: * size() Get the number of MAP elements* isEmpty() Determine whether MAP is empty* clear() Delete all elements of MAP* put(key, value) Add elements (key, value) to MAP * remove(key) Delete elements of the specified KEY, return True successfully, return False if failed * get(key) Get the element value of the specified KEY, return NULL if failed * element(index) Get the element of the specified index (use element.key, element.value to get KEY and VALUE if used), return NULL if failed * containsKey(key) Determine whether the element of the specified KEY is contained in the MAP* containsValue(value) Determine whether the MAP contains elements with the specified VALUE* values() Get an array of all VALUEs in MAP (ARRAY) * keys() Get an array of all KEYs in MAP (ARRAY) * * Example: * var map = new Map(); * * map.put("key", "value"); * var val = map.get("key") * … * */function hashmap() {/*** Store data*/this.data = new Object();/*** Put a key-value pair* @param {String} key* @param {Object} value*/this.put = function(key, value) {this.data[key] = value;};/*** Get the value corresponding to a key* @param {String} key* @return {Object} value*/this.get = function(key) {return this.containsKey(key)?this.data[key]:null; };/*** Delete a key-value pair* @param {String} key*/this.remove = function(key) {delete this.data[key];};/*** Traverse the Map and execute the processing function* * @param {Function} Callback function function(key,value,index){..}*/this.each = function(fn){if(typeof fn != 'function'){return;}var len = this.data.length;for(var i=;i<len;i++){var k = this.data[i];fn(k,this.data[k],i);}};/*** Get key value array (similar to Java's entrySet())* @return Array of key value object {key,value}*/this.entrys = function() {var len = this.data.length;var entrys = new Array(len);for (var i = ; i < len; i++) {entrys[i] = {key : i,value : this.data[i]};}return entrys;};/**** Determine whether the Map is empty*/this.isEmpty = function() {return this.data.length == ;};/*** Get the number of key-value pairs*/this.size = function(){return this.data.length;};/*** Rewrite toString and install it in JSON format*/this.toString = function(){var s = "[";for(var i=;i<this.data.length;i++,s+=','){var k = this.data[i];s += "{'id':'" + k+"','value':'"+this.data[k]+"'}";}s=s.substring(, s.length-);if(s!=""){s+="]";}return s;};/*** The value of output value*/this.values = function (){var _values= new Array(); for(var key in this.data){ _values.push(this.data[key]); } return _values; };/*** Get keys*/this.keySet = function (){var _keys = new Array(); for(var key in this.data){ _keys.push(key); } return _keys; };/*** Determine whether the MAP contains the element with the specified KEY*/this.containsKey = function(_key){ return (_key in this.data); }; /** * Clear Map */ this.clear = function(){ this.data.length = ;this.data = new Object();}; }The above is the relevant knowledge of the detailed explanation of the js hashMap example introduced by the editor. I hope it will be helpful to everyone!