Simulate the Map object using JS, and simulate the map object using js.

Source: Internet
Author: User

Simulate the Map object using JS, and simulate the map object using js.

A Map method provided by the supporter for a project to be implemented recently is quite usable and has a slight lack of functions. Therefore, I have extended it. The following code is used:

Function Map () {this. elements = new Array (); // obtain the number of MAP elements this. size = function () {return this. elements. length ;}; // determines 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) this to the MAP. put = function (_ key, _ value) {this. elements. push ({key: _ key, value: _ value}) ;}; // Delete the element of the specified KEY. True is returned for success, False is returned for failure. 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. True is returned successfully, false returned for failure. 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. True is returned successfully, false returned for failure. 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 ;}; // gets the element VALUE of the specified KEY, return NULL for failure. 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 ;}; // gets the element of the specified index (using element. key, element. value to get the KEY and VALUE), return NULL this if the failure. element = function (_ index) {if (_ index <0 | _ index> = this. elements. length) {return null;} return this. elements [_ index] ;}; // determines whether the MAP contains the element of the specified KEY. 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 ;}; // obtain the key using 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 <arr. length; j ++) {if (arr [j] = this. elements [I]. key) {flag = false; break;} if (flag) {arr. push (this. elements [I]. key) ;}} return arr ;};}

Existing features:

/* Size () gets the number of MAP elements
* IsEmpty () determines whether the MAP is empty.
* Clear () deletes all MAP elements.
* Add an element (key, value) to the MAP using put (key, value)
* Remove (key) deletes the element of the specified KEY. If the key is successfully deleted, True is returned. If the KEY is failed, False is returned.
* Get (key) gets the element VALUE of the specified KEY. If the VALUE fails, NULL is returned.
* Element (index) obtains the elements of the specified index (using element. key, element. value to obtain KEY and VALUE). If the element fails, NULL is returned.
* ContainsKey (key) determines whether the MAP contains elements of the specified KEY.
* ContainsValue (value) determines whether the MAP contains the element of the specified VALUE.
* Values () obtains the ARRAY (ARRAY) of all values in the MAP)
* Keys () obtains the ARRAY (ARRAY) of all keys in the MAP)

*/

For java Map, functions may still be insufficient, especially the put method, which cannot be automatically re-arranged, that is, if the key already exists, it will always exist, that is to say, the key may not be unique and the value will not be overwritten. This is obviously different from the Map we know. Therefore, we need to make an extension:

Var myMap = {getMapInstance: function () {var map = new Map ();/*** if the key already exists, overwrite value */map. putNoRepeat = function (_ key, _ value) {if (this. containsKey (_ key) {for (I = 0; I <this. elements. length; I ++) {if (this. elements [I]. key = _ key) {this. elements [I]. value = _ value; break ;}} else {this. put (_ key, _ value) ;}} return map ;}};
In this way, the above Map is inherited, And the deduplication method such as putNoRepeat is used, only the following is required:

var map = myMap.getMapInstance();
In this way, both the extended method and unnecessary memory overhead can be used.


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.