Customize a Map class in JS

Source: Internet
Author: User
Tags map class

Example 1

// Define a simple Map

Function getMap () {// initialize map _ and add a method to the map _ OBJECT To make map _ look like Map

Var map _ = new Object ();

Map _. put = function (key, value ){

Map _ [key + '_'] = value;

};

Map _. get = function (key ){

Return map _ [key + '_'];

};

Map _. remove = function (key ){

Delete map _ [key + '_'];

};

Map _. keyset = function (){

Var ret = "";

For (var p in map _){

If (typeof p = 'string' & p. substring (p. length-1) = "_"){

Ret + = ",";

Ret + = p. substring (0, p. length-1 );

}

}

If (ret = ""){

Return ret. split (",");

} Else {

Return ret. substring (1). split (",");

}

};

Return map _;

}

Var map = getMap ();

Map. put ("395", ", 89, 35 ");

Map. put ("396", "121111,2222221, 5333332,8444449, 3555555 ");

Alert (map. get ("395"); // output: 12, 21, 35

Alert (map. keyset (); // output: 395,396



Example 2

Function HashMap (){

This. map = {};

}

HashMap. prototype = {

Put: function (key, value ){

This. map [key] = value;

},

Get: function (key ){

If (this. map. hasOwnProperty (key )){

Return this. map [key];

}

Return null;

},

Remove: function (key ){

If (this. map. hasOwnProperty (key )){

Return delete this. map [key];

}

Return false;

},

RemoveAll: function (){

This. map = {};

},

KeySet: function (){

Var _ keys = [];

For (var I in this. map ){

_ Keys. push (I );

}

Return _ keys;

}

};

HashMap. prototype. constructor = HashMap;

Var hashMap = new HashMap ();

HashMap. put ('key', 'value ');

HashMap. put ('key1', 'value ');

Console. log (hashMap. get ('key '));

Console. log (hashMap. keySet ());

Console. log (hashMap. remove ('key '));

Console. log (hashMap. keySet ());


Example 3

Function Map (){
This. elements = new Array ();

// Obtain the number of MAP elements
This. size = function (){
Return this. elements. length;
};

// Determine whether the MAP is empty
This. isEmpty = function (){
Return (this. elements. length <1 );
};

// Delete All MAP elements
This. clear = function (){
This. elements = new Array ();
};

// Add the element (key, value) to the MAP)
This. put = function (_ key, _ value ){
This. elements. push ({
Key: _ key,
Value: _ value
});
};

// Delete the element of the specified KEY. If the KEY is successfully deleted, True is returned. If the KEY fails to be deleted, False is returned.
This. removeByKey = 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;
};

// Delete the element of the specified VALUE. If the element is successfully deleted, True is returned. If the element fails, False is returned.
This. removeByValue = function (_ value) {// removeByValueAndKey
Var bln = false;
Try {
For (I = 0; I <this. elements. length; I ++ ){
If (this. elements [I]. value = _ value ){
This. elements. splice (I, 1 );
Return true;
}
}
} Catch (e ){
Bln = false;
}
Return bln;
};

// Delete the element of the specified VALUE. If the element is successfully deleted, True is returned. If the element fails, False is returned.
This. removeByValueAndKey = function (_ key, _ value ){
Var bln = false;
Try {
For (I = 0; I <this. elements. length; I ++ ){
If (this. elements [I]. value = _ value & this. elements [I]. key = _ key ){
This. elements. splice (I, 1 );
Return true;
}
}
} Catch (e ){
Bln = false;
}
Return bln;
};

// Obtain the element VALUE of the specified KEY. If the VALUE fails, NULL is returned.
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 false;
}
Return false;
};

// Obtain the elements of the specified index (using element. key, element. value to obtain KEY and VALUE). If the index fails, NULL is returned.
This. element = function (_ index ){
If (_ index <0 | _ index> = this. elements. length ){
Return null;
}
Return this. elements [_ index];
};

// Determine whether the MAP contains the specified KEY element.
This. containsKey = function (_ 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 whether the MAP contains the specified VALUE element.
This. containsValue = function (_ value ){
Var bln = false;
Try {
For (I = 0; I <this. elements. length; I ++ ){
If (this. elements [I]. value = _ value ){
Bln = true;
}
}
} Catch (e ){
Bln = false;
}
Return bln;
};

// Determine whether the MAP contains the specified VALUE element.
This. containsObj = function (_ key, _ value ){
Var bln = false;
Try {
For (I = 0; I <this. elements. length; I ++ ){
If (this. elements [I]. value = _ value & this. elements [I]. key = _ key ){
Bln = true;
}
}
} Catch (e ){
Bln = false;
}
Return bln;
};

// Obtain the ARRAY (ARRAY) of all values in the MAP)
This. values = function (){
Var arr = new Array ();
For (I = 0; I <this. elements. length; I ++ ){
Arr. push (this. elements [I]. value );
}
Return arr;
};

// Obtain the ARRAY (ARRAY) of all values in the MAP)
This. valuesByKey = function (_ key ){
Var arr = new Array ();
For (I = 0; I <this. elements. length; I ++ ){
If (this. elements [I]. key = _ key ){
Arr. push (this. elements [I]. value );
}
}
Return arr;
};

// Obtain the ARRAY (ARRAY) of all keys in the MAP)
This. keys = function (){
Var arr = new Array ();
For (I = 0; I <this. elements. length; I ++ ){
Arr. push (this. elements [I]. key );
}
Return arr;
};

// Get key through value
This. keysByValue = function (_ value ){
Var arr = new Array ();
For (I = 0; I <this. elements. length; I ++ ){
If (_ value = this. elements [I]. value ){
Arr. push (this. elements [I]. key );
}
}
Return arr;
};

// Obtain the ARRAY (ARRAY) of all keys in the MAP)
This. keysRemoveDuplicate = function (){
Var arr = new Array ();
For (I = 0; I <this. elements. length; I ++ ){
Var flag = true;
For (var j = 0; j if (arr [j] = this. elements [I]. key ){
Flag = false;
Break;
}
}
If (flag ){
Arr. push (this. elements [I]. key );
}
}
Return arr;
};
}


The second method is easy to use. I recommend the second method.

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.