1.Javascriptmap output
function Map () {//private var obj = {};//Empty object container, load key value pair//put method this.put = function (key, value) {Obj[key] = value;//Key value pair Binds the}//size method on the Obj object to get the number of map containers this.size = function () {var count = 0; for (var attr in obj) {count++;} return count; }//Get method gets valuethis.get = function (key) {if (Obj[key] | | obj[key] = = 0 | | obj[key] = = = False) {return obj[key];} el SE {return null;}} Remove Delete Method This.remove = function (key) {if (Obj[key] | | obj[key] = = 0 | | obj[key] = = = False) {delete Obj[key];}} Eachmap variable Map container method This.eachmap = function (fn) {for (var attr in obj) {fn (attr, obj[attr]);}}} Simulate Mapvar m = new Map in Java, m.put (' ', ' abc '), M.put (' n ', ' false); M.put (' ', true); M.put (' n ', New Date ());// Alert (M.size ());//alert (' m.get ');//m.remove (' ');//alert (' m.get '); M.eachmap (function (key, value) {Alert (Key + ":" + value);});
2. Remove duplicates from map
var arr = [2,1,2,10,2,3,5,5,1,10,13];//object//js object features: In the JS object The key is never repeated/*var obj = new Object (); obj.name = ' Z3 '; obj . age = 1;//alert (obj.name); obj.name = ' W5 '; alert (obj.name); *///convert the array to a JS object//2 the value in the array into the JS object key//3 To restore the object to an array/ /convert Array to Object function Toobject (arr) {var obj = {};//Private object Var J; for (Var i=0, j= arr.length; i<j; i++) {obj[arr[i]] = Tru e;} return obj;} Turn this object into a group function keys (obj) {var arr = [];///Private object for (Var attr in obj) {if (Obj.hasownproperty (attr)) {//yui underlying code Arr.push ( attr);}} return arr;} The combined method removes duplicates in the array function Uniq (newarr) {return keys (Toobject (Newarr));} Alert (Uniq (arr));
Javacript analog Map output and remove duplicates