Javacript analog Map output and remove duplicates

Source: Internet
Author: User

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

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.