JavaScript數(shù)組去重方法合集

一、方法

方法一:先進(jìn)行原數(shù)組升序排序,然后對(duì)比相鄰元素

Array.prototype.distinct1 = function () {
    console.time('distinct1');       //測(cè)試性能的相關(guān)代碼
    var temp = this.sort(function (a, b) {
        return a-b;
    });
    var res = [temp[0]];
    for(var i=0; i<temp.length;i++){
        if(temp[i] !== res[res.length-1]){
            res.push(temp[i]);
        }
    }
    console.timeEnd('distinct1');
    console.log(res);
};

方法二:利用對(duì)象屬性唯一性

Array.prototype.distinct2 = function () {
    console.time('distinct2');        //測(cè)試性能的相關(guān)代碼
    var res=[],obj={};
    for(var i=0; i<this.length;i++){
        if(!obj[this[i]]){
            res.push(this[i]);
            obj[this[i]]=1;
        }
    }
    console.timeEnd('distinct2');
    console.log(res);
};

方法三:利用數(shù)組indexOf方法

Array.prototype.distinct3 = function () {
    console.time('distinct3');        //測(cè)試性能的相關(guān)代碼
    var res=[];
    for(var i=0; i<this.length;i++){
        if(res.indexOf(this[i])===-1){
            res.push(this[i]);
        }
    }
    console.timeEnd('distinct3');
    console.log(res);
};

方法四:利用數(shù)組includes方法

Array.prototype.distinct4 = function () {
    console.time('distinct4');        //測(cè)試性能的相關(guān)代碼
    var res=[];
    for(var i=0; i<this.length;i++){
        if(!res.includes(this[i])){
            res.push(this[i]);
        }
    }
    console.timeEnd('distinct4');
    console.log(res);
};

方法五:利用數(shù)組forEach、includes方法

Array.prototype.distinct5 = function () {
    console.time('distinct5');        //測(cè)試性能的相關(guān)代碼
    var res=[];
    this.forEach(function (value) {
        if(!res.includes(value)){
            res.push(value);
        }
    });
    console.timeEnd('distinct5');
    console.log(res);
};

方法六:利用ES6 Set方法

Array.prototype.distinct6 = function () {
    console.time('distinct6');       //測(cè)試性能的相關(guān)代碼
    let res = Array.from(new Set(this))
    console.timeEnd('distinct6');
    console.log(res);
};

二、性能

var data = [];
for(var j=0;j<1000000;j++){
    data.push(Math.round(Math.random()*15));
}
data.distinct1(); // [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]  221ms
data.distinct2(); // [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]  5ms
data.distinct3(); // [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]  19ms
data.distinct4(); // [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]  20ms
data.distinct5(); // [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]  44ms
data.distinct6(); // [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ]  77ms

三、總結(jié)

1、對(duì)比方法四、方法五,可知盡量少用forEach,而應(yīng)該用for循環(huán)代替。
2、對(duì)比運(yùn)行時(shí)間,方法二所需時(shí)間最短,性能最優(yōu)。
3、有其他好的方法,歡迎大家在底下留言,我會(huì)補(bǔ)上去的。


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容