JavaScript implements key-value pairs "map"

Source: Internet
Author: User

javascript  does not have a map, but has a map function-_-!  do it yourself, clothed (function () {try{function directory () {this.key =  new array (); This.value = new array ();} Add Directory.prototype.add = function (key,value) {//key whether to repeat for (var i=0;i<) with the already existing key this.key.length;i++) {if (This.key[i] == key) { throw new error ("duplicate key");  }}//is not repeated, Addthis.key.push (key); This.value.push (value);} Gets Directory.prototype.get = function (key) {for (var i=0;i<this.key.length;i++) {if (this.key[i ] == key) return this.value[i];} Throw new error ("Nothing You Look for  referenceerror");  }//Delete directory.prototype.remove =  function (key) {for (var i=0;i<this.key.length;i++) {if (This.key[i] == key) {// Delete element This.key.splice (i,1) at position I, this.value.splice (i,1); return true;}}  throw new error ("Without you deleting the yarn  referenceerror"); }var myarr = new  Directory (); Myarr.add ("123"); Myarr.add ("Wo", "Bysowhat"), Alert (Myarr.get ("Wo")), Alert (Myarr.get ("123")), Myarr.remove ("123"), Alert (Myarr.get ("123"));} catch (E) {alert (e.message);}}) ();



Complete!

Reproduced....

/* * map objects, implementing map functions  * *  Interfaces:  * size ()       getting the number of map elements  * isempty ()      determine if map is empty  * clear ()       Delete all elements of map  * put (key, value)     add elements to map (Key, value)   *  Remove (key)      Delete the element with the specified key, return true successfully, fail return False * get (key)      Gets the value of the element for the specified key, failed to return Null * element (index)     Gets the element of the specified index (using Element.key,element.value to get the key and value), and fails back to Null * containskey (key)    Determines whether the map contains the specified key element  * containsvalue (value)   Determines if the map contains an element of the specified value  * values ()       get an array of all the value in map (array)  * keys ()       get an array of all the keys in the map (array)  * *  Example:  * var map = new map ();  * * map.put ("Key",   "value")  * var val = map.get ("key")  *&nbsP;...... * */function map ()  {    this.elements = new array ( );      //gets the number of map elements     this.size = function ()  {         return this.elements.length;    }      //determine if the map is empty     this.isempty = function ()  {         return  (this.elements.length < 1);     }     //Delete all elements of map     this.clear =  function ()  {        this.elements = new array ();     }     //adding elements to the map (key, value)       this.put = function (_key, _value)  {         This.elements.push (&NBSP;{&NBSP;&Nbsp;          key : _key,             value : _value         });     }     //deletes the element of the specified key, returns true successfully, fails to return 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 value of the element for the specified key, failed to return 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 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];    }      //Determines whether the map contains the element     this.containskey = function of the specified key (_key )  {        var bln = 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) &NBSP;{&NBSP;&NBSP;&NBSP;&NBsp;                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++)  {          &nbSp;  arr.push (This.elements[i].value);        }         return arr;    }     // Gets an array of all the 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;     }}





Creating a new Error object:

Internet Explorer supports the following form:

var errorobj = new Error ([number[, description]]);

Number
Optional. Integer that specifies the error code. Default is 0. Sets the error property of the error object.

message
Optional. String that specifies the message of the error. Default is an empty string. Sets the description and messageproperties of the Errorobject.

Firefox supports the following form:

var errorobj = new Error ();

message  
Optional. < Strong style= "margin:0px;padding:0px;" >string  that Specifies the message of the error. Default is an empty string. Sets The message Properties of The error object.

filename  
Optional. < Strong style= "margin:0px;padding:0px;" >string  that Specifies the name of the the file where the error occurs. The Default is the file where The error  object is created. Sets The error  object.

linenumber  
Optional. < Strong style= "margin:0px;padding:0px;" >integer  that Specifies the line number where the error occurs. Default is the line number, where The error object is created. Sets Theerror  object.

Opera, Google Chrome and Safari support the following form:

var errorobj = new Error ([message]);

message
Optional. String that specifies the message of the error. Default is an empty string. Sets the description and messageproperties of the Error object.


JavaScript implements key-value pairs "map"

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.