_javascript Techniques for implementing Map function code sharing in JavaScript

Source: Internet
Author: User
/*   * Map object, implement map function    *   * interface:   * size ()      get the number of map elements &
nbsp  * IsEmpty ()     determine if map is empty    * clear ()      Delete all elements of map    * Put (key, value)    Add the element (key, value)     * remove (key) to the map     Delete the element of the specified key. Returns true successfully, Failure returns false   * get (key)     Gets the element value value of the specified key, failure returns null   * Element (index)    gets the element of the specified index (using Element.key,element.value to get key and value), Failure returns null   * ContainsKey (key)   Determines whether the map contains elements of the specified key    * containsvalue (value) to determine if the map contains elements of the specified value    * values ()    
Gets an array of all the value in the map    * keys ()      gets an array of all key in the map (array)    *   * example:   * var map = new map ();   *   * map.put ("Key", "value");   * var val = Map.get ("key")    * ......   *   */   FUnction Map () {       this.elements = new Array ();               //Get the number of map elements        this.size = function () {            return this.elements.length;     
  }              //To determine if map is empty        this.isempty = function () {            Return (This.elements.length < 1);       }               //Delete map all elements        this.clear = function () {            this.elements = new Array ();        }              //adds elements to the map (key, value)         this.put = function (_key, _value) {            this.elements.push ({                key: _key,                value: _value          &nbsp);        }              //Deletes the element of the specified key. Successfully returns TRUE, 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;                    }               }           &nbsp} catch (e) {                bln = false;            }           return bln;        }              //Gets the element value of the specified key, Failed to return to NULL&NBsp;      this.get = function (_key) {            try{               for (i = 0; i < This.elements.length; i++) {                    if (This.elements[i].key = = _key) {                        return this.elements[i].value;                    }                }         &NBSP;&NBSP;&NBSP} catch (e) {                return null;   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSp     }              // Gets the element of the specified index (using Element.key,element.value to get the key and value), and the failure returns null       this.element = function (_index) {           if (_index < 0 | | _index >= This.elements.length) {                return null;           }            return this.elements[_index];       }               //Determine if the map contains elements of the specified key         This.containskey = function (_key) {           varbln = false;            try{           &Nbsp;    for (i = 0; i < this.elements.length i++) {        
           if (This.elements[i].key = = _key) {                        bln = true;                    }               }           &nbsp} catch (e) {                bln = false;            }           return bln;        }              //Determines whether the map contains elements of the specified value & NBsp;      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 the array (array)        this.keys = function () {     of all key keys in the map  &Nbsp;     var arr = new Array ();            for (i = 0; i < this.elements.length i++) {                arr.push (this.elements[i].key);            }           return arr;        }  }

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.