JavaScript MAP Implementation

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 && Key! = "") this.container[ Key] = value;} catch (e) {return e;}};/ /Remove the corresponding value from the MAP according to key Map.prototype.get = function (key) {try {return this.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.cont Ainer[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;elsereturn false;};/ /Gets the size of Map Map.prototype.size = function () {return this.keyset (). length;} Returns an 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;} 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;} 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.container[keys[i]);} return array;}  Returns the and of the value values in the map (valid for the Nunmber type) Map.prototype.sumValues = function () {var values = this.values (); var result = 0;for (var i = 0; i < values.length; i++) {result + = number (Values[i]);} return result;} Calculates the percent value of two integers, function topercent (num, total) {num = parsefloat (num); total = parsefloat (total); if (IsNaN (num) | | IsNaN (Total) {return ' 0 ';} return total <= 0? "0%": (Math.Round (Num/total * 10000)/100.00 + "%"); Return (Math.Round (this * 10000)/100). ToFixed (2) + '% ';}

  

JavaScript MAP Implementation

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.