Map object to implement the map function

Source: Internet
Author: User
/*** Map object to implement the map function *** size () to obtain the number of map elements * isempty () to determine whether the map is empty * clear () delete all map elements * put (Key, value) add an element (Key, value) * remove (key) to map to delete the elements of the specified key. If yes, true is returned, failed to return false * Get (key) to get the element value of the specified key, failed to return NULL * element (INDEX) to get the element of the specified index (using element. key, element. returns NULL * containskey (key) to determine whether the map contains the element * containsvalue (value) of the specified key) determine whether the map contains the element * keys () of the specified value to obtain the Array (array) * values () of all keys in the map to obtain the Array (array) of all values in the map) */Function Map () {This. elements = new array (); // obtain the number of map elements this. size = function () {return this. elements. length ;}, // determine whether the map is null 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) {If (this. containskey (_ key) = true) {If (this. containsvalue (_ value) {If (this. remove (_ key) = true) {This. elements. push ({key: _ key, value: _ value}) ;}} else {This. elements. push ({key: _ key, value: _ value}) ;}} else {This. elements. push ({key: _ key, value: _ value}) ;}}, // Delete the element of the specified key. True is returned for success, and falsethis is returned for failure. remove = 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 ;}, // gets the element value of the specified key, failed to return nullthis. 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 NULL ;}, // gets the element of the specified index (using element. key, element. value to obtain the key and value). If the return value fails, nullthis is returned. 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 ;}, // 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 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;};/*** rewrite tostring */This. tostring = function () {var S = ""; for (I = 0; I <this. elements. length; I ++) {S + = This. elements [I]. value + "" ;}return s ;};}

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.