Map implemented by javascript (combined with jquery)

Source: Internet
Author: User
First, implement the remove Method of Array: Map is implemented based on array. This code is written by a person. During my test, the remove Method of map cannot be used, so I wrote the above array. remove Method viewplaincopytoclipboardprint? FunctionMap () {... Synt

First, implement the remove Method of Array:

 

Map implementation is based on array. This code is written by a person. During the test, the remove Method of map is not available, so the above array. remove Method is written.

View plaincopy to clipboardprint? Function Map (){
/** Array for storing keys (used for traversal )*/
This. keys = new Array ();
/** Store data */
This. data = new Object ();

/**
* Put a key-Value Pair
* @ Param {String} key
* @ Param {Object} value
*/
This. put = function (key, value ){
If (this. data [key] = null ){
This. keys. push (key );
}
This. data [key] = value;
};

/**
* Obtain the value of a key.
* @ Param {String} key
* @ Return {Object} value
*/
This. get = function (key ){
Return this. data [key];
};

/**
* Delete a key-Value Pair
* @ Param {String} key
*/
This. remove = function (key ){
This. keys. remove (key );
This. data [key] = null;
};

/**
* Traverse the Map and execute the processing function.
*
* @ Param {Function} callback function Function (key, value, index ){..}
*/
This. each = function (fn ){
If (typeof fn! = 'Function '){
Return;
}
Var len = this. keys. length;
For (var I = 0; I Var k = this. keys [I];
Fn (k, this. data [k], I );
}
};

/**
* Get the key-value array (similar to Java's entrySet ())
* @ Return: array of {key, value} key value object
*/
This. entrys = function (){
Var len = this. keys. length;
Var entrys = new Array (len );
For (var I = 0; I <len; I ++ ){
Entrys [I] = {
Key: this. keys [I],
Value: this. data [I]
};
}
Return entrys;
};

/**
* Determines whether the Map is empty.
*/
This. isEmpty = function (){
Return this. keys. length = 0;
};

/**
* Get the number of key-value pairs
*/
This. size = function (){
Return this. keys. length;
};

}
Function Map (){
/** Array for storing keys (used for traversal )*/
This. keys = new Array ();
/** Store data */
This. data = new Object ();

/**
* Put a key-Value Pair
* @ Param {String} key
* @ Param {Object} value
*/
This. put = function (key, value ){
If (this. data [key] = null ){
This. keys. push (key );
}
This. data [key] = value;
};

/**
* Obtain the value of a key.
* @ Param {String} key
* @ Return {Object} value
*/
This. get = function (key ){
Return this. data [key];
};

/**
* Delete a key-Value Pair
* @ Param {String} key
*/
This. remove = function (key ){
This. keys. remove (key );
This. data [key] = null;
};

/**
* Traverse the Map and execute the processing function.
*
* @ Param {Function} callback function Function (key, value, index ){..}
*/
This. each = function (fn ){
If (typeof fn! = 'Function '){
Return;
}
Var len = this. keys. length;
For (var I = 0; I Var k = this. keys [I];
Fn (k, this. data [k], I );
}
};

/**
* Get the key-value array (similar to Java's entrySet ())
* @ Return: array of {key, value} key value object
*/
This. entrys = function (){
Var len = this. keys. length;
Var entrys = new Array (len );
For (var I = 0; I <len; I ++ ){
Entrys [I] = {
Key: this. keys [I],
Value: this. data [I]
};
}
Return entrys;
};

/**
* Determines whether the Map is empty.
*/
This. isEmpty = function (){
Return this. keys. length = 0;
};

/**
* Get the number of key-value pairs
*/
This. size = function (){
Return this. keys. length;
};

}

Author: "huilixiang's column"
 

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.