Note: Please indicate the source for reprinting!
/** ------------------------ Public method -------------------------*/
/**
* Add the Remove Method for Array
*
* @ Param s
* Removed values in the array
* @ Return
*/
Array. Prototype. Remove = function (s ){
For (VAR I = 0; I <this. length; I ++ ){
If (S = This [I])
This. splice (I, 1 );
}
}
/** ------------------------ Public method end -------------------------*/
/** ------------------------ Bytemap start -------------------------*/
/**
* Simulate Java map class, put (Key, value), get (key), remove (key), isempty ,..........
*/
Function bytemap (){
/** Store key-value data in keys */
VaR keys = new array ();
VaR DATA = new object ();
/**
* Put a key-Value Pair
*
* @ Param key
* Value: String
* @ Param Value
* Key value object
*/
This. Put = function (Key, value ){
If (data [Key] = NULL ){
Keys. Push (key );
}
Data [Key] = value;
};
/**
* Obtain the key value of a key position
*
* @ Param key
* Key string
*/
This. Get = function (key ){
Return date [Key];
};
/**
* If a key does not exist, "-1" is returned"
*
* @ Param key
* Key string
*/
This. Remove = function (key ){
// When the key does not exist
If (data [Key] = NULL ){
Return '-1 ';
}
Keys. Remove (key );
Data [Key] = NULL;
};
/**
* When the FN (Key, value, index) parameter of the callback function is not a function, ruturn "-1"
*
* @ Param FN
* Callback function name the function parameter should be (Key, value, index)
*/
This. Each = function (FN ){
// Check whether this function exists
If (typeof FN! = 'Function '){
Return '-1 ';
}
For (VAR I = 0; I <keys. length; I ++ ){
FN (Keys [I], data [Keys [I], I );
}
};
/**
* The array corresponding to the key value is similar to the Java entryset ()
*
* @ Return: array of {key, value} key value object
*/
This. entryarray = function (){
VaR Len = keys. length;
VaR temparr = new array (LEN );
For (VAR I = 0; I <Len; I ++ ){
Temparr [I] = {
Key: Keys [I],
Value: Data [Keys [I]
};
}
Return temparr;
};
/**
* The returned key array is similar to the Java ketset () function.
*
* @ Return Array
*/
This. keyset = function (){
Return keys;
}
/**
* Determines whether the map is empty.
*
* @ Return NULL return true not null return false
*/
This. isempty = function (){
Return keys. Length = 0;
};
/**
* Returns the map size.
*/
This. size = function (){
Return keys. length;
};
/**
* Rewrite tostring format to {key1 = value1, key2 = value2 ,....}
*/
This. tostring = function (){
VaR Len = keys. length;
VaR temp = "{";
For (VAR I = 0; I <Len; I ++ ){
Temp + = (Keys [I] + "=" + data [Keys [I] + ",");
}
Temp + = "}";
Return temp;
};
/**
* Clear map
*
* @ Return
*/
This. Clear = function (){
For (VAR I = keys. Length-1; I> = 0; I --){
Delete data [Keys [I];
Keys. Pop (I );
}
};
}
/** ------------------------ Bytemap test method start -------------------------*/