This article introduces the sample code for map implementation in javascript. the code is easy to understand and can be used to obtain and delete key values. if you are interested, follow the script house editor. 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. data [key] ;};/*** delete a key-value pair * @ param {String} key */this. remove = function (key) {for (var I = 0; I
0;};/*** rewrite toString */this. toString = function () {var s = "{"; for (var I = 0; I
I + 1) {s + = ','} s + = "}"; return s ;};/*** parse the string to Map * {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; i0 & 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
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.