Simple Map implementation in javascript

Source: Internet
Author: User

Simple Map implementation in javascript

This article describes how to use javascript to implement a simple Map. You can obtain, Judge, delete, and add a map.

The Code is as follows:

/*

* 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. 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 <arr. length; j ++ ){

If (arr [j] = this. elements [I]. key ){

Flag = false;

Break;

}

}

If (flag ){

Arr. push (this. elements [I]. key );

}

}

Return arr;

};

}

 

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.