Write a map with JavaScript

Source: Internet
Author: User

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

Related Article

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.