JavaScript notes-Application of object properties

Source: Internet
Author: User

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

Related Article

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.