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.