Using Array and Object in JavaScript to implement Map _ javascript skills

Source: Internet
Author: User
This article describes how to use Array and Object to implement Map in JavaScript. The example shows how to add, retrieve, remove, clear, and traverse map in javascript, it has some reference value. If you need it, you can refer to the example below to describe how to use Array and Object to implement Map in JavaScript. Share it with you for your reference. The details are as follows:

Yesterday, I suddenly saw that the Map implemented by others using JavaScript was very good, but I found that some methods have problems. By the way, I improved the methods, such as remove, indexOf, values, and clear.

/*** @ Author blune68 * @ version 0.1, 07/27/12 **/function Map () {this. keys = new Array (); this. data = new Object (); var toString = Object. prototype. toString;/*** current Map length */this. size = function () {return this. keys. length;}/*** add value * @ param {Object} key * @ param {Object} value */this. put = function (key, value) {if (this. data [key] = null) {this. data [key] = value;} this. keys. push (key );}/* ** Obtain the value * @ param {Object} key */this based on the current key. get = function (key) {return this. data [key];}/*** remove the Map value based on the current key * @ param {Object} key */this. remove = function (key) {var index = this. indexOf (key); if (index! =-1) {this. keys. splice (index, 1);} this. data [key] = null;}/*** clear Map */this. clear = function () {for (var I = 0, len = this. size (); I <len; I ++) {var key = this. keys [I]; this. data [key] = null;} this. keys. length = 0;}/*** whether the current key exists * @ param {Object} key */this. containsKey = function (key) {return this. data [key]! = Null;}/*** whether it is null */this. isEmpty = function () {return this. keys. length = 0;}/*** type Map in Java. entrySet */this. entrySet = function () {var size = this. size (); var datas = new Array (size); for (var I = 0, len = size; I <len; I ++) {var key = this. keys [I]; var value = this. data [key]; datas [I] = {'key': key, 'value': value} return datas ;} /*** traverse the current Map * var map = new Map (); * map. put ('key', 'value'); * map. each (function (index, key, value) {* console. log ("index:" + index + "-- key:" + key + "-- value:" + value) *}) * @ param {Object} fn */this. each = function (fn) {if (toString. call (fn) = '[object Function]') {for (var I = 0, len = this. size (); I <len; I ++) {var key = this. keys [I]; fn (I, key, this. data [key]) ;}} return null;}/*** get the current key index value in Map * @ param {Object} key */this. indexOf = function (key) {var size = this. size (); if (size> 0) {for (var I = 0, len = size; I <len; I ++) {if (this. keys [I] = key) return I;} return-1;}/*** Override toString */this. toString = function () {var str = "{"; for (var I = 0, len = this. size (); I <len; I ++, str + = ",") {var key = this. keys [I]; var value = this. data [key]; str + = key + "=" + value;} str = str. substring (0, str. length-1); str + = "}"; return str;}/*** get all values in the Map (Array) */this. values = function () {var size = this. size (); var values = new Array (); for (var I = 0; I <size; I ++) {var key = this. keys [I]; values. push (this. data [key]) ;}return values ;}}

I hope this article will help you design javascript programs.

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.