This article describes the method of javascript to simulate map output and remove duplicates. Share it for your reference. The specific methods are as follows:
1. Javascriptmap output
function Map(){ // private var obj = {} ;// empty object container, hosting key-value pairs // put method this.put = function(key , value){ obj[key] = value ;// Bind the key-value pair to the obj object} // size method obtains the number of map containers this.size = function(){ var count = 0 ; for(var attr in obj){ count++; } return count ; } // get method obtains value based on key this.get = function(key){ if(obj[key] || obj[key] === 0 || obj[key] === false){ return obj[key]; } else { return null; } } //remove Delete method this.remove = function(key){ if(obj[key] || obj[key] === 0 || obj[key] === false){ delete obj[key]; } } // Method of eachMap variable map container this.eachMap = function(fn){ for(var attr in obj){ fn(attr, obj[attr]); } } } // Simulate Map var m = 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 duplicates in map
var arr = [2,1,2,10,2,3,5,5,1,10,13];//object // Characteristics of js object: keys are never repeated in js objects/* var obj = new Object(); obj.name = 'z3'; obj.age = 20 ; //alert(obj.name); obj.name = 'w5'; alert(obj.name); */ // 1 Convert the array into a js object// 2 Convert the value in the array to the key in the js object // 3 Restore the object to the array// Convert the array into an object function toObject(arr){ var obj = {} ; // Private object var j ; for(var i=0 , j= arr.length ; i<j; i++){ obj[arr[i]] = true ; } return obj ; } // Convert this object into an array function keys(obj){ var arr = [] ; // Private object for(var attr in obj){ if(obj.hasOwnProperty(attr)){// YUI underlying code arr.push(attr); } } return arr ; } // Comprehensive method removes duplicates in array function uniq(newarr){ return keys(toObject(newarr)); } alert(uniq(arr));I hope this article will be helpful to everyone's JavaScript programming.