日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Array 数组去重 总结10方法(7)

發布時間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Array 数组去重 总结10方法(7) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1,常規雙循環去重(缺點:循環次數較多)

?

Array.prototype.unique1 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this),len = that.length >>> 0;let res = [that[0]];for(let i = 1; i < len; i++){let falg = false;for(let j = 0; j < res.length; j++){if(that[i] === res[j]){falg = true;break;}}if(!falg){res.push(this[i]);}}return res;
}

注意:

?

(1,必須在第二個循環外push到新的數組

(2,減少循環次數,在第二個循環中找到相等值,馬上退出該循環

(3,每次循環對falg檢驗

(4,由于第一值直接賦值,所以不用檢測第一個值

2,數組的sort先排序再去重(缺點:返回數組為排序后的順序)

?

Array.prototype.unique2 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this).sort(),len = that.length >>> 0,res = [that[0]];for(let i = 1; i < len; i++){if(that[i] !== res[res.length - 1]){res.push(that[i]);}}return res;
}

3,對象鍵值不重復(缺點:占內存)

?

?

Array.prototype.unique3 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this),len = that.length >>> 0,obj = {},res = [];for(let i = 0; i < len; i++){let type = typeof that[i];if(!obj[that[i]]){res.push(that[i]);obj[that[i]] = [type];}else if(obj[that[i]].indexOf(type) === -1){res.push(that[i]);obj[that[i]].push(type);}}return res;
}


4,indexOf檢測新數組(優點:簡單)

?

?

Array.prototype.unique4 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this),len = that.length >>> 0,res = [that[0]];for(let i = 1; i < len; i++){if(res.indexOf(that[i]) === -1){res.push(that[i]);}}return res;
}

5,indexOf檢測原數組

Array.prototype.unique5 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this),len = that.length >>> 0,res = [that[0]];for(let i = 1; i < len; i++){if(that.indexOf(that[i]) === i){res.push(that[i]);}}return res;
}


6,數組的every方法

?

?

Array.prototype.unique6 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this),len = that.length >>> 0,res = [that[0]];for(let i = 1; i < len; i++){if(res.every(function(val){return val !== that[i]})){res.push(that[i]);}}return res;
}

注意:如果發現了一個這樣的元素,every 方法將會立即返回 false。否則,callback 為每一個元素返回 true,every 就會返回 true。

?

7,數組的some方法

?

Array.prototype.unique10 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this),len = that.length >>> 0,res = [that[0]];for(let i = 1; i < len; i++){if(!res.some(function(val){return val === that[i]})){res.push(that[i]);}}return res;
}

注意:如果找到了這樣一個值,some 將會立即返回 true。否則,some 返回 false。
8,數組的includes方法

?

?

Array.prototype.unique7 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this),len = that.length >>> 0,res = [that[0]];for(let i = 1; i < len; i++){if(!res.includes(that[i])){res.push(that[i]);}}return res;
}

9,數組的filter方法

?

?

Array.prototype.unique8 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this),len = that.length >>> 0,res = [that[0]];for(let i = 1; i < len; i++){if(res.filter(function(val){return val === that[i]}).length === 0){res.push(that[i]);}}return res;
}

10,數組的find方法(缺點:沒有做0或者false這個一類判斷)

?

?

Array.prototype.unique9 = function(){if(this === null){throw new TypeError('"this" is null or not defined');}let that = Object(this),len = that.length >>> 0,res = [that[0]];for(let i = 1; i < len; i++){if(!res.find(function(val){return val === that[i]})){res.push(that[i]);}}return res;
}

還有lastIndexOf,findIndex等方法也能做去重,就不一一列舉,有興趣的可以自己做一下。

其他

[我的博客,歡迎交流!](http://rattenking.gitee.io/stone/index.html)

[我的CSDN博客,歡迎交流!](https://blog.csdn.net/m0_38082783)

[微信小程序專欄](https://blog.csdn.net/column/details/18335.html)

[前端筆記專欄](https://blog.csdn.net/column/details/18321.html)

[微信小程序實現部分高德地圖功能的DEMO下載](http://download.csdn.net/download/m0_38082783/10244082)

[微信小程序實現MUI的部分效果的DEMO下載](http://download.csdn.net/download/m0_38082783/10196944)

[微信小程序實現MUI的GIT項目地址](https://github.com/Rattenking/WXTUI-DEMO)

[微信小程序實例列表](http://blog.csdn.net/m0_38082783/article/details/78853722)

[前端筆記列表](http://blog.csdn.net/m0_38082783/article/details/79208205)

[游戲列表](http://blog.csdn.net/m0_38082783/article/details/79035621)



?

?

轉載于:https://www.cnblogs.com/linewman/p/9918546.html

總結

以上是生活随笔為你收集整理的Array 数组去重 总结10方法(7)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。