1 functionMap () {2 This. elements =NewArray ();3 4 //get the number of map elements5 This. Size =function() {6 return This. elements.length;7 }8 9 //determine if the map is emptyTen This. IsEmpty =function() { One return( This. elements.length < 1); A } - - //Delete all elements of map the This. Clear =function() { - This. elements =NewArray (); - } - + //add a unique element to the map (key, value) - This. put =function(_key, _value) { + This. Remove (_key); A This. Elements.push ({ at key: _key, - Value: _value - }); - } - - //add duplicate elements to map (key, value) in This. Putrepeat =function(_key, _value) { - This. Elements.push ({ to key: _key, + Value: _value - }); the } * $ //deletes the element of the specified key, returns true successfully, and fails to return falsePanax Notoginseng This. remove =function(_key) { - varbln =false; the Try { + for(i = 0; i < This. elements.length; i++) { A if( This. Elements[i].key = =_key) { the This. Elements.splice (I, 1); + return true; - } $ } $}Catch(e) { -bln =false; - } the returnbln; - }Wuyi the //gets the element value of the specified key, and the failure returns null - This. get =function(_key) { Wu Try { - varresult =NULL; About for(i = 0; i < This. elements.length; i++) { $ if( This. Elements[i].key = =_key) { -result = This. Elements[i].value; - } - } A returnresult; +}Catch(e) { the return NULL; - } $ } the the //sets the value of the specified key element in the map, and fails to return null the This. Set =function(_key, _value) { the Try { - This. Remove (_key); in This. Put (_key, _value); the}Catch(e) { the return NULL; About } the } the the //gets the element of the specified index (using Element.key,element.value to get the key and value), and fails to return null + This. Element =function(_index) { - if(_index < 0 | | _index >= This. Elements.length) { the return NULL;Bayi } the return This. Elements[_index]; the } - - //determine if the map contains an element of the specified key the This. ContainsKey =function(_key) { the varbln =false; the Try { the for(i = 0; i < This. elements.length; i++) { - if( This. Elements[i].key = =_key) { thebln =true; the } the }94}Catch(e) { thebln =false; the } the returnbln;98 } About - //Determines whether the map contains an element of the specified value101 This. Containsvalue =function(_value) {102 varbln =false;103 Try {104 for(i = 0; i < This. elements.length; i++) { the if( This. Elements[i].value = =_value) {106bln =true;107 }108 }109}Catch(e) { thebln =false;111 } the returnbln;113 } the the //get an array of all the value in the map (array) the This. Values =function() {117 vararr =NewArray ();118 for(i = 0; i < This. elements.length; i++) {119Arr.push ( This. Elements[i].value); - }121 returnarr;122 }123 124 //gets an array of all keys in the map (array) the This. Keys =function() {126 vararr =NewArray ();127 for(i = 0; i < This. elements.length; i++) { -Arr.push ( This. Elements[i].key);129 } the returnarr;131 } the}
JavaScript Implementation Map