Javascript method for simulating map output and removing repeated items _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to simulate map output and remove repeated items in javascript, and implements the function of removing repeated items through user-defined functions combined with traversal and deletion, which is of great practical value, for more information about how to simulate map output by using javascript and how to remove repeated items, see the example in this article. Share it with you for your reference. The specific method is as follows:

1. Javascriptmap output

Function Map () {// private var obj = {}; // empty object container, which is used to load the key-value pair // put method this. put = function (key, value) {obj [key] = value; // bind the key-value pair to the obj object} // size method to obtain the number of map containers this. size = function () {var count = 0; for (var attr in obj) {count ++;} return count ;}// get Method get value this according to key. get = function (key) {if (obj [key] | obj [key] = 0 | obj [key] = false) {return obj [key] ;}else {return null ;}// remove the delete method this. remove = function (key) {if (obj [key] | obj [key] = 0 | obj [key] = false) {delete obj [key] ;}// method of the map container of the eachMap variable this. eachMap = function (fn) {for (var attr in obj) {fn (attr, obj [attr]) ;}} // simulate Map var m in java = new Map (); m. put ('01', 'ABC'); m. put ('02', false); m. put ('03', true); m. put ('04 ', new Date (); // alert (m. size (); // alert (m. get ('02'); // m. remove ('03'); // alert (m. get ('03'); m. eachMap (function (key, value) {alert (key + ":" + value );});

2. remove repeated items in map

Var arr = [, 13]; // object // Features of js objects: in js objects, keys are never repeated./* var obj = new Object (); obj. name = 'z3'; obj. age = 20; // alert (obj. name); obj. name = 'w5'; alert (obj. name); * // 1 converts the array into a js object // 2 converts the value in the array, convert it to the key/3 in the js object and then restore the object to an array // Convert the array to the object function toObject (arr) {var obj = {}; // private object var j; for (var I = 0, j = arr. length; I
 
  

I hope this article will help you design javascript programs.

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.