JavaScript implements the map structure

Source: Internet
Author: User

Define map function Map () {this.container = {};} Put Key-value into Map Map.prototype.put = function (key, value) {try {if (key! = null) {This.container[key] = value;}} Cat CH (e) {return e;}};/ /Remove the corresponding value from the MAP according to key Map.prototype.get = function (key,deft) {if (!this.containskey (key)) {return deft;} try {return thi S.container[key]; } catch (e) {return e;}};/ /Determine if the MAP contains the specified key Map.prototype.containsKey = function (key) {try {for (var p in This.container) {if (p = = key) return True } return false; } catch (e) {return e;}} Determines whether the Map contains the specified value Map.prototype.containsValue = function (value) {try {for (var p in This.container) {if (This.conta Iner[p] = = = value) return true; } return false; } catch (e) {return e;}};/ /delete the specified key Map.prototype.remove = function (key) {try {delete This.container[key] in Map,} catch (e) {return e;}};/ /empty Map Map.prototype.clear = function () {try {delete this.container; this.container = {};} catch (e) {return e;}};/ /Determine if Map is empty Map.prototype.isEmpty = FunCtion () {if (This.keyset (). Length = = 0) return true; else return false;};/ /Gets the size of Map Map.prototype.size = function () {return this.keyset (). length;} Returns the array of key values in the Map Map.prototype.keySet = function () {var keys = new Array (); for (var p in This.container) {Keys.push (P); } return keys; Traverse MapMap.prototype.each = function (fun) {var keys = This.keyset (), for (var i = 0;i < keys.length;i++) {fun (keys[i],th Is.get (Keys[i])); }}//returns an array of values in Map Map.prototype.values = function () {var valuesarray = new Array (); var keys = This.keyset (); for (Var i = 0; i < keys.length; i++) {Valuesarray.push (this.container[keys[i]);} return valuesarray;} Gets the maximum value of the map, the parameter is the comparison function Map.prototype.max = function (Compare) {var keys = This.keyset (); var maxkey = Keys[0],maxvalue = THIS.G ET (keys[0]); for (var i = 0;i < keys.length;i++) {if (Compare (This.get (keys[i],maxvalue))) {MaxValue = This.get (Keys[i]); Maxkey = key S[i]; }} return [Maxkey,maxvalue];} Returns the EntrySet object in Map Map.prototype.entrySet = function (){var array = new Array (); var keys = This.keyset (); for (var i = 0; i < keys.length; i++) {Array.push (keys[i],this.co Ntainer[keys[i]]); } return array;

JavaScript implements the map structure

Related Article

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.