You can simulate the map type and remove array duplicates based on the principle that key is never repeated in the JS object .
1. Analog map Type (1) construct map object
function Map () {//privatevar obj = {};//Empty object container, storing key value pairs //put Method This. put = function (Key,value) {Obj[key] = value; }//get Method This. get = function (key) {if(Obj[key] | | obj[key] = = 0 | | obj[key] = = =false){returnObj[key]; }Else{return NULL; } }//remove Method This. remove = function (key) {if(Obj[key] | | obj[key] = = 0 | | obj[key] = = =false) {delete Obj[key]; } }//size Method This. Size = function () {var count= 0; for(Var arr in obj) {count++; }returnCount }//Traversal method, using the callback function of the way This. Eachmap = function (fn) { for(Var arr in obj) {fn (Arr,obj[arr]); } }}
(2) test code
//Simulate the type of map in Java New Map (); M.put (' n ', ' Sscs '); Alert (M.size ()); Alert (m.get (' 01 ')); M.remove (' 01 '); //Traverse the object element, the passed parameter is a function M.eachmap (key,value) { alert (key + ":" +value);});
2. Remove array duplicates (1) Implementation code
function Unique (arr) { //First step: Change the value in the array to the key //Private Object in the JS object for (var i = 0,j = arr.length;i<j;i++) { true; } //second: Restore the JS object to an array var arr = []; //Private Object for (var attr in obj) { if(Obj.hasownproperty (attr)) { arr.push (attr); } } return arr;}
(2) test procedure
var arr = [1,1,3,4,5];alert (Unique (arr)); Show 1,3,4,5
JavaScript notes-Application of object properties