Js imitates the Java Map class

Source: Internet
Author: User
Tags map class

Var Map = function () {var datas = new Array (); // Add the element this. put = function (key, value) {var obj = this. get (key); if (obj! = Null) {obj. value = value;} else {datas. push ({key: key, value: value}) ;}}; this. putAll = function (map) {for (var item in map. iterator () {this. put (map. iterator () [item]. key, map. iterator () [item]. value) ;}// clear this. clear = function () {datas = new Array () ;}; // iterator this. iterator = function () {return datas ;}; // gets the element this. get = function (key) {for (var item in datas) {if (datas [item]. key = key) {return datas [item] ;}} return null ;}; // obtain all values this. values = function () {var values = new Array (); for (var item in datas) {values. push (datas [item]. value);} return values;}; // obtain all keys this. keys = function () {var values = new Array (); for (var item in datas) {values. push (datas [item]. key) ;}return values ;}; // number of elements this. size = function () {return datas. length ;};}; var map = new Map (); map. put ('zhang san', 'haha '); map. put ('zhao liu', 'you are '); var map2 = new Map (); map2.put ('zhao liu2', 'haha'); map2.put ('wang wu ', 'haha '); map. putAll (map2); alert (map. size (); alert (map. keys (); alert (map. values ());


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.