Tag: text NULL index false size script new get element
<script type= "Text/javascript" >functionMap () { This. elements =NewArray (); } //get the number of map elementsMap.prototype.size =function() { return This. Elements.length; }; //determine if the map is emptyMap.prototype.isEmpty =function() { return( This. elements.length < 1); }; //Delete all elements of mapMap.prototype.clear =function() { This. elements =NewArray (); }; //adding elements to a map (key, value)Map.prototype.put =function(_key, _value) {if( This. elements.length>0) { varflag=true; for(vari = 0; I < This. elements.length; i++) { if( This. elements[i].key==_key) { This. elements[i].value=_value; Flag=false; } }; if(flag) { This. Elements.push ({key: _key, Value: _value}); }; }Else{ This. Elements.push ({key: _key, Value: _value}); }; }; //deletes the element of the specified key, returns true successfully, and fails to return falseMap.prototype.remove =function(_key) {varbln =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; } returnbln; }; //gets the element value of the specified key, and the failure returns nullMap.prototype.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), and fails to return nullMap.prototype.element =function(_index) {if(_index < 0 | | _index >= This. Elements.length) { return NULL; } return This. Elements[_index]; }; //determine if the map contains an element of the specified keyMap.prototype.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; } returnbln; }; //Determines whether the map contains an element of the specified valueMap.prototype.containsValue =function(_value) {varbln =false; Try { for(i = 0; i < This. elements.length; i++) { if( This. Elements[i].value = =_value) {bln=true; } } } Catch(e) {bln=false; } returnbln; }; //get an array of all the value in the map (array)Map.prototype.getValues =function() { vararr = []; for(i = 0; i < This. elements.length; i++) {Arr.push ( This. Elements[i].value); } returnarr; }; //gets an array of all keys in the map (array)Map.prototype.getKeys =function() { vararr = []; for(i = 0; i < This. elements.length; i++) {Arr.push ( This. Elements[i].key); } returnarr; };</script>
Use JavaScript to imitate Java map function, group training work, record!
Write a map with JavaScript