Simple implementation of JS Map and List

Source: Internet
Author: User

/*
* MAP object to implement the MAP function
*
* Interface:
* 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)
*
* Example:
* Var map = new Map ();
*
* Map. put ("key", "value ");
* Var val = map. get ("key ")
*......
*
*/
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. 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;
};

// 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 null;
}
};

// 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;
};

// 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 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;
};
}

 

 

From Moving

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.