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"