Sample Code for Map implementation in JavaScript, and sample code for map

Source: Internet
Author: User

Sample Code for Map implementation in JavaScript, and sample code for map

No nonsense. paste the Code directly.

Code 1:

Var map = new Map (); map. put ("a", "A"); map. put ("B", "B"); map. put ("c", "C"); map. get ("a"); // return value: Amap. entrySet () // returns Entity [{key, value}, {key, value}] map. containsKey ('kevin ') // return: false
Function Map () {this. keys = new Array (); this. data = new Object ();/*** put a key-value Pair * @ param {String} key * @ param {Object} value */this. put = function (key, value) {if (this. data [key] = null) {this. keys. push (key); this. data [key] = value;} else {this. data [key] = this. data [key] ;}return true ;};/*** obtain the value of a key * @ param {String} key * @ return {Object} value */this. get = function (key) {return this. dat A [key] ;};/*** delete a key-Value Pair * @ param {String} key */this. remove = function (key) {for (var I = 0; I <this. keys. length; I ++) {if (key = this. keys [I]) {var del_keys = this. keys. splice (I, 1); for (k in del_keys) {this. data [k] = null;} return true;} return false;};/*** traverse Map, execute the processing Function ** @ param {function} callback Function function (key, value, index ){..} */this. each = function (fn) {if (typeof fn! = 'Function') {return;} var len = this. keys. length; for (var I = 0; I <len; I ++) {var k = this. keys [I]; fn (k, this. data [k], I) ;}};/*** get the key value array * @ return entity [{key, value}, {key, value}] */this. entrySet = function () {var len = this. keys. length; var entrys = new Array (len); for (var I = 0; I <len; I ++) {entrys [I] = {key: this. keys [I], value: this. data [this. keys [I]};} return entrys;};/*** judge Ma Whether p is null */this. isEmpty = function () {return this. keys. length = 0;};/*** obtain the number of key-value pairs */this. size = function () {return this. keys. length ;}; this. containsKey = function (key) {return this. keys. filter (function (v) {if (v = key) {return key ;}}). length> 0;};/*** rewrite toString */this. toString = function () {var s = "{"; for (var I = 0; I <this. keys. length; I ++) {var k = this. keys [I]; s + = k + "=" + this. data [k]; if (t His. keys. length> I + 1) {s + = ','} s + = "}"; return s ;}; /*** parse the string to Map * {a = A, B = B, c = B,} */this. parserStringAndAddMap = function (str) {var count = 0; if (str & str. length> 0) {str = str. trim (); var startIndex = str. indexOf ("{"), endIndex = str. lastIndexOf ("}"); if (startIndex! =-1 & endIndex! =-1) {str = str. substring (startIndex + 1, endIndex); var arrs = str. split (","); for (var I = 0; I <arrs. length; I ++) {var kv = arrs [I]. trim (); if (kv. length> 0 & kv. indexOf ("= ")! =-1) {var kv_arr = kv. split ("="); if (kv_arr.length = 2) {if (this. put (kv_arr [0]. trim (), kv_arr [1]. trim () {count ++;} else {console. error ('error: kv: '+ kv) ;}}} else {console. log ("data error:" + str) ;}} else {console. log ('data is not empty');} return count ;};}

Code 2:

Array. prototype. remove = function (s) {for (var I = 0; I <this. length; I ++) {if (s = this [I]) this. splice (I, 1) ;}}/*** Simple Map *** var m = new Map (); * m. put ('key', 'value ');*... * var s = ""; * m. each (function (key, value, index) {* s + = index + ":" + key + "=" + value + "\ n ";*}); * alert (s); ** @ author dewitt * @ date 2008-05-24 */function Map () {/** array for storing keys (used for traversal) */this. keys = new Array ();/** Store Data */this. data = new Object ();/*** put a key-value Pair * @ param {String} key * @ param {Object} value */this. put = function (key, value) {if (this. data [key] = null) {this. keys. push (key);} this. data [key] = value ;};/*** get the value of a key * @ param {String} key * @ return {Object} value */this. get = function (key) {return this. data [key] ;};/*** delete a key-Value Pair * @ param {String} key */this. remove = function (key) {thi S. keys. remove (key); this. data [key] = null ;};/*** traverses the Map and executes the processing Function ** @ param {function} callback Function (key, value, index ){..} */this. each = function (fn) {if (typeof fn! = 'Function') {return;} var len = this. keys. length; for (var I = 0; I <len; I ++) {var k = this. keys [I]; fn (k, this. data [k], I) ;}};/*** get the key-value array (similar to Java's entrySet () * @ return key-value object {key, value} array */this. entrys = function () {var len = this. keys. length; var entrys = new Array (len); for (var I = 0; I <len; I ++) {entrys [I] = {key: this. keys [I], value: this. data [I] };} return entrys ;};/*** checks whether the Map is empty */this. isEmpty = function () {return this. keys. length = 0;};/*** obtain the number of key-value pairs */this. size = function () {return this. keys. length ;};/*** rewrite toString */this. toString = function () {var s = "{"; for (var I = 0; I <this. keys. length; I ++, s + = ',') {var k = this. keys [I]; s + = k + "=" + this. data [k];} s + = "}"; return s ;};}
Function testMap () {var m = new Map (); m. put ('key1', 'comtop'); m. put ('key2', 'southgrid '); m. put ('key3', 'landscape Garden '); alert ("init:" + m); m. put ('key1', 'extenp'); alert ("set key1:" + m); m. remove ("key2"); alert ("remove key2:" + m); var s = ""; m. each (function (key, value, index) {s + = index + ":" + key + "=" + value + "\ n ";}); alert (s );}

The above content is shared with you through two pieces of code to implement Map in JavaScript. I hope you will like it.

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.