Map using JavaScript

Source: Internet
Author: User

Var map = new Map ();

Map. put ("a", "A"); map. put ("B", "B"); map. put ("c", "C ");

Map. get ("a"); // return:

Map. 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 ;};}


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.