JS some topics

Source: Internet
Author: User

1. The key inside the object does not distinguish the string

var a={};a[1]= "a"; a["1"]=2;console.log (a);

  

2, expand the array prototype, write a method to remove the array duplicates

//algorithm one, two-layer loop, poor performance Array.prototype.unique = function () {var len = this.length,    I        22 comparison (array length greater than 1) while (--len > 0) {i = Len;                while (-i >= 0) {//Previous index min 0 if (this[len] = = = This[i]) {//last item compared to previous item            This.splice (i, 1);            This.length automatically minus one, deleting the previous duplicates This.splice (Len, 1);            This.length automatically minus one, delete the following duplicate item i--; }}} return this;};/        /algorithm two, performance optimization Array.prototype.unique = function () {var i, key, Len = this.length, cache = {},//Cached object    ret = [];        The array to return for (i = 0; i < len; i++) {key = typeof This[i] + this[i];            if (Cache[key]!== 1) {Cache[key] = 1;        Ret.push (This[i]); }} return ret;;};/    /workaround three, directly using the Jquery.unique tool function var arr = [1,3,4,6,9,10,4,6];arr = Jquery.unique (arr); [1, 3, 4, 6, 9, ten] 

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.