JavaScript implementation of MAP functions (reprint)

Source: Internet
Author: User

/* Map object, implement map function * * Interface: * SIZE () Get number of map elements * ISEMPTY () Determine if map is empty * Clear () Delete map all elements * put (key , value) adds an element (key, value) * Remove (key) to the map to remove the element of the specified key, which returns true successfully, and the failure returns false * GET (key) gets the element value of the specified key, and the failed return Nu LL * Element (index) Gets the element of the specified index (using Element.key,element.value to get the key and value), and the failure returns NULL * CONTAINSKEY (key) to determine if the map contains a specified key in the element Element * Containsvalue (value) to determine if the map contains the elements of the specified value * VALUES () gets an array of all value in the map (array) * keys () gets an array of all the keys in the map (ARRA   Y) * * Example: * var map = new map ();   * * Map.put ("key", "value");            * var val = map.get ("key") * * * */function map () {this.elements = new Array ();       Gets the number of map elements this.size = function () {return this.elements.length;       }//Determine if map is empty this.isempty = function () {return (This.elements.length < 1);       }//delete map all elements this.clear = function () {this.elements = new Array (); }//Add elements to the map (keY, value) this.put = function (_key, _value) {This.elements.push ({key: _key,       Value: _value});           }//Deletes the element of the specified key, returns true successfully, and the failure returns false 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, and the failure returns null 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 the EleMent.key,element.value gets key and value), failure returns null this.element = function (_index) {if (_index < 0 | | _index &           Gt;= this.elements.length) {return null;       } return This.elements[_index];           }//Determine if the map contains the element with 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 if the map contains an element of the specified value 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;           }//Gets an array of all the value in the map (array) this.values = function () {var arr = new Array ();           for (i = 0; i < this.elements.length; i++) {Arr.push (this.elements[i].value);       } return arr;           }//Gets an array of all keys in the map (array) This.keys = function () {var arr = new Array ();           for (i = 0; i < this.elements.length; i++) {Arr.push (This.elements[i].key);       } return arr; }   }

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.