Code sharing for map implementation in JavaScript, javascriptmap
/** MAP object to implement the MAP function ** interface: * size () gets the number of MAP elements * isEmpty () to determine whether the MAP is blank * clear () delete all MAP elements * put (key, value) add an element (key, value) * remove (key) to MAP to delete the elements of the specified KEY. If yes, True is returned, failed to return False * get (key) to get the element VALUE of the specified KEY, failed to return NULL * element (index) to get the element of the specified index (using element. key, element. returns NULL * containsKey (KEY) to determine whether the MAP contains the element * containsValue (value) of the specified key) determine whether the MAP contains the element * values () of the specified VALUE to obtain the ARRAY (ARRAY) of all values in the MAP) * Keys () obtains the ARRAY (ARRAY) of all keys in the MAP. ** example: * var map = new Map (); ** map. put ("key", "value"); * var val = map. get ("key ")*...... **/Function Map () {this. elements = new Array (); // obtain the number of MAP elements this. size = function () {return this. elements. length;} // determine whether the MAP is null this. isEmpty = function () {return (this. elements. length <1);} // Delete All MAP elements this. clear = function () {this. elements = new Array ();} // Add the element (key, value) this to the MAP. put = function (_ key, _ value) {this. elements. push ({key: _ key, value: _ value}) ;}// Delete the element of the specified KEY. True is returned for success, False is returned for failure. this. remove = function (_ key) {var bln = false; try {for (I = 0; I <this. elements. length; I ++) {if (this. elements [I]. key = _ key) {this. elements. splice (I, 1); return true ;}} catch (e) {bln = false;} return bln ;}// gets the element VALUE of the specified KEY, return NULL for failure. this. get = function (_ key) {try {for (I = 0; I <this. elements. length; I ++) {if (this. elements [I]. key = _ key) {return this. elements [I]. value ;}} catch (e) {return null ;}// gets the element of the specified index (using element. key, element. value to get the KEY and VALUE), return NULL this if the failure. element = function (_ index) {if (_ index <0 | _ index> = this. elements. length) {return null;} return this. elements [_ index];} // determines whether the MAP contains the element of the specified KEY. this. containsKey = function (_ key) {varbln = false; try {for (I = 0; I <this. elements. length; I ++) {if (this. elements [I]. key = _ key) {bln = true ;}} catch (e) {bln = false;} return bln ;} // determine whether the MAP contains the specified VALUE element this. containsValue = function (_ value) {var bln = false; try {for (I = 0; I <this. elements. length; I ++) {if (this. elements [I]. value = _ value) {bln = true ;}} catch (e) {bln = false;} return bln ;}// obtain the ARRAY (ARRAY) of all values in the MAP) this. values = function () {var arr = new Array (); for (I = 0; I <this. elements. length; I ++) {arr. push (this. elements [I]. value);} return arr;} // obtain the ARRAY (ARRAY) of all keys in the MAP this. keys = function () {var arr = new Array (); for (I = 0; I <this. elements. length; I ++) {arr. push (this. elements [I]. key) ;}return arr ;}}