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)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 德萨罗人鱼封面是谁画的呢?
- 下一篇: 第92步兵师、黑人兵团、福克斯分队是啥电