Copy codeThe Code is as follows:
/**
*
* Description: map method implemented by js.
* @ Returns {Map}
*/
Function Map (){
Var struct = function (key, value ){
This. key = key;
This. value = value;
};
// Add a map key-Value Pair
Var put = function (key, value ){
For (var I = 0; I <this. arr. length; I ++ ){
If (this. arr [I]. key = key ){
This. arr [I]. value = value;
Return;
}
};
This. arr [this. arr. length] = new struct (key, value );
};
// Obtain value based on key
Var get = function (key ){
For (var I = 0; I <this. arr. length; I ++ ){
If (this. arr [I]. key = key ){
Return this. arr [I]. value;
}
}
Return null;
};
// Delete by key
Var remove = function (key ){
Var v;
For (var I = 0; I <this. arr. length; I ++ ){
V = this. arr. pop ();
If (v. key = key ){
Continue;
}
This. arr. unshift (v );
}
};
// Obtain the number of map key-value pairs
Var size = function (){
Return this. arr. length;
};
// Determine whether the map is empty
Var isEmpty = function (){
Return this. arr. length <= 0;
};
This. arr = new Array ();
This. get = get;
This. put = put;
This. remove = remove;
This. size = size;
This. isEmpty = isEmpty;
}
The usage is the same as the Map class in java.
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var map = new Map ();
Map. put ("num", 1 );
Map. put ("ss", "wss ");
Alert (map. size ());
Alert (map. get ("num "));
ParaArr. remove ("num ");
Alert (map. size ());
Alert (map. get ("num "));
</Script>