js一些題目

來源:互聯網
上載者:User

標籤:blog   java   使用   javascript   cti   演算法   

1、對象裡面的key不區分出string

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

  

2、擴充Array原型,編寫一個去除數組重複項的方法

 

// 演算法一,兩層迴圈,效能差Array.prototype.unique = function(){    var len = this.length, i;    // 兩兩比較(數組長度大於1)    while ( --len > 0 ) {        i = len;        while ( --i >= 0 ) {                    // 前一項索引最小為0            if ( this[len] === this[i] ) {      // 最後一項與前一項進行比較                //this.splice(i, 1);            // this.length自動減一,刪除前面的重複項                this.splice(len, 1);            // this.length自動減一,刪除後面的重複項                i--;            }        }    }    return this;};// 演算法二,效能最佳化Array.prototype.unique = function(){    var i, key, len = this.length,        cache = {},        // 緩衝對象        ret = [];    // 要返回的數組    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;;};// 解決方案三,直接使用jQuery.unique工具函數var arr = [1,3,4,6,9,10,4,6];arr = jQuery.unique( arr );    // [1, 3, 4, 6, 9, 10]

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.