Array 数组去重 总结10方法(7)
1,常規(guī)雙循環(huán)去重(缺點(diǎn):循環(huán)次數(shù)較多)
?
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,必須在第二個(gè)循環(huán)外push到新的數(shù)組
(2,減少循環(huán)次數(shù),在第二個(gè)循環(huán)中找到相等值,馬上退出該循環(huán)
(3,每次循環(huán)對(duì)falg檢驗(yàn)
(4,由于第一值直接賦值,所以不用檢測(cè)第一個(gè)值
2,數(shù)組的sort先排序再去重(缺點(diǎn):返回?cái)?shù)組為排序后的順序)
?
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,對(duì)象鍵值不重復(fù)(缺點(diǎn):占內(nèi)存)
?
?
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檢測(cè)新數(shù)組(優(yōu)點(diǎn):簡(jiǎn)單)
?
?
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檢測(cè)原數(shù)組
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,數(shù)組的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;
} 注意:如果發(fā)現(xiàn)了一個(gè)這樣的元素,every 方法將會(huì)立即返回 false。否則,callback 為每一個(gè)元素返回 true,every 就會(huì)返回 true。
?
7,數(shù)組的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;
} 注意:如果找到了這樣一個(gè)值,some 將會(huì)立即返回 true。否則,some 返回 false。
8,數(shù)組的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,數(shù)組的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,數(shù)組的find方法(缺點(diǎn):沒有做0或者false這個(gè)一類判斷)
?
?
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;
} 還有l(wèi)astIndexOf,findIndex等方法也能做去重,就不一一列舉,有興趣的可以自己做一下。
其他
[我的博客,歡迎交流!](http://rattenking.gitee.io/stone/index.html)
[我的CSDN博客,歡迎交流!](https://blog.csdn.net/m0_38082783)
[微信小程序?qū)赸(https://blog.csdn.net/column/details/18335.html)
[前端筆記專欄](https://blog.csdn.net/column/details/18321.html)
[微信小程序?qū)崿F(xiàn)部分高德地圖功能的DEMO下載](http://download.csdn.net/download/m0_38082783/10244082)
[微信小程序?qū)崿F(xiàn)MUI的部分效果的DEMO下載](http://download.csdn.net/download/m0_38082783/10196944)
[微信小程序?qū)崿F(xiàn)MUI的GIT項(xiàng)目地址](https://github.com/Rattenking/WXTUI-DEMO)
[微信小程序?qū)嵗斜韂(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)
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/linewman/p/9918546.html
總結(jié)
以上是生活随笔為你收集整理的Array 数组去重 总结10方法(7)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 德萨罗人鱼封面是谁画的呢?
- 下一篇: 第92步兵师、黑人兵团、福克斯分队是啥电