JS HashMap Example detailed _javascript skills

Source: Internet
Author: User

The

HashMap is a very common, widely used type of data. This article through the example code to introduce the relevant knowledge of JS HashMap, the specific code content as follows:

 /** * Map object, realize map function * Interface: * Size () Get the number of map elements * IsEmpty () to determine whether the map is empty * Clear () Delete all elements of the map * put (key, value) to add to the map The element (key, value) * Remove (key) deletes the element of the specified key, returns true successfully, fails returns false * GET (key) gets the element value value of the specified key, and the failure returns NULL * ELEMENT (index) gets the specified index Element (using Element.key,element.value to get key and value), the failure returns NULL * CONTAINSKEY (key) to determine if the map contains the element * Containsvalue (value) of the specified key  
To determine if the map contains the specified value element * VALUES () gets the array (array) * keys () for all values in the map to obtain an array (array) * Example of all keys in the map: * 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 * @para
m {String} key * @param {Object} value */this.put = function (key, value) {This.data[key] = value;}; /** * Get the value of 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 map to perform processingFunctions * * @param {Function} callback function (Key,value,index) {.} */This.each = function (fn) {if (typeof fn!= ' function ') {RE
Turn
var len = this.data.length;
for (Var i=;i<len;i++) {var k = this.data[i]; FN (k,this.data[k],i);}}; /** * Gets the array of key values (Java-like EntrySet ()) * @return The array of key-value objects {Key,value}/This.entrys = function () {var len = this.data.length; va
R Entrys = new Array (len);
for (var i =; i < len; i++) {Entrys[i] = {key:i, value:this.data[i]};
};
/** * To determine if the map is null/this.isempty = function () {return this.data.length = =;
/** * Gets the key value pair quantity * * This.size = function () {return this.data.length;};
/** * Rewrite toString, in JSON format */this.tostring = function () {var s = ' []; 
for (Var i=;i<this.data.length;i++,s+= ', ') {var k = this.data[i]; s = = ' {' id ': ' + + + ', ' 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 the keys */this.keyset = function () {var _keys = new Array (); 
for (var key in This.data) {_keys.push (key); 
return _keys;
}; 
/** * To determine if the map contains the elements of the specified key/This.containskey = function (_key) {return (_key in this.data); 
}; 
/** * Empty map */this.clear = function () {this.data.length =; this.data = new Object (); }

The above is a small set to introduce you to the JS HashMap examples of relevant knowledge, I hope to help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.