Sample Code for map method implemented by js _ javascript tips-js tutorial

Source: Internet
Author: User
Tags map class
This article introduces the map method implemented by js. The example is as follows. The Code is as follows:


/**
*
* Description: map method implemented by js.
* @ Returns {Map}
*/
Function Map (){
Var struct = function (key, value ){
This. key = key;
This. value = value;
};
// Add a map key-Value Pair
Var put = function (key, value ){
For (var I = 0; I <this. arr. length; I ++ ){
If (this. arr [I]. key = key ){
This. arr [I]. value = value;
Return;
}
};
This. arr [this. arr. length] = new struct (key, value );
};
// Obtain value based on key
Var get = function (key ){
For (var I = 0; I <this. arr. length; I ++ ){
If (this. arr [I]. key = key ){
Return this. arr [I]. value;
}
}
Return null;
};
// Delete by key
Var remove = function (key ){
Var v;
For (var I = 0; I <this. arr. length; I ++ ){
V = this. arr. pop ();
If (v. key = key ){
Continue;
}
This. arr. unshift (v );
}
};
// Obtain the number of map key-value pairs
Var size = function (){
Return this. arr. length;
};
// Determine whether the map is empty
Var isEmpty = function (){
Return this. arr. length <= 0;
};
This. arr = new Array ();
This. get = get;
This. put = put;
This. remove = remove;
This. size = size;
This. isEmpty = isEmpty;
}


The usage is the same as the Map class in java.

The Code is as follows:


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.