今天就跟大家聊聊有关使用JavaScript对数组去重的方法,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
一,通過尋找對象屬性
var n = [14,12,2,2,2,5,32,2,59,5,6,33,12,32,6];
Array.prototype.unique1 = function(){
var obj = {},
ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(!obj[this[i]]){
obj[this[i]] = 1;
ary.push(this[i]);
}
}
return ary.sort(function(a,b){return a - b});
}
console.log(n.unique1());
二,通過尋找數組位置
var n = [14,12,2,2,2,5,32,2,59,5,6,33,12,32,6];
Array.prototype.unique2 = function(){
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(ary.indexOf(this[i]) == -1) ary.push(this[i]);
}
return ary.sort(function(a,b){return a - b});
}
console.log(n.unique2());
三,跟尋找數組位置類似,搜索數字第一次出現的位置是不是跟當前位置一樣
var n = [14,12,2,2,2,5,32,2,59,5,6,33,12,32,6];
Array.prototype.unique3 = function(){
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(this.indexOf(this[i]) == i) ary.push(this[i]);
}
return ary.sort(function(a,b){return a - b});
}
console.log(n.unique3());
四,比較常規,先排序,再比較前後兩個數字是不是相等
var n = [14,12,2,2,2,5,32,2,59,5,6,33,12,32,6];
Array.prototype.unique4 = function(){
this.sort(function(a,b){return a - b});
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(this[i] !== this[i-1]) ary.push(this[i]);
}
return ary;
}
console.log(n.unique4());
接下來是網上比較少的效率比較了,也是本文重點,我們先生成一個計時函數統一比較
首先生成一個一百數字的數組
Array.prototype.unique1 = function(){
var obj = {},
ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(!obj[this[i]]){
obj[this[i]] = 1;
ary.push(this[i]);
}
}
return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique2 = function(){
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(ary.indexOf(this[i]) == -1) ary.push(this[i]);
}
return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique3 = function(){
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(this.indexOf(this[i]) == i) ary.push(this[i]);
}
return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique4 = function(){
this.sort(function(a,b){return a - b});
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(this[i] !== this[i-1]) ary.push(this[i]);
}
return ary;
}
function randomAry (n) {
var ary = [],i=0;
for(; i<n; i++){
ary.push(Math.ceil(Math.random()*10000));
}
console.log(ary)
return ary;
}
function useTime (fn) {
var start = new Date();
fn();
var end = new Date();
console.log('本次函數運行花了:' + (end - start) + '毫秒');
}
var ary = randomAry(100),
fn1 = function(){
ary.unique1()
},
fn2 = function(){
ary.unique2()
},
fn3 = function(){
ary.unique3()
},
fn4 = function(){
ary.unique4()
};
useTime(fn1);
useTime(fn2);
useTime(fn3);
useTime(fn4);
結果在我的谷歌瀏覽器發現
方法一:0毫秒
方法二:1毫秒
方法三:0毫秒
方法四:00毫秒
(嗯,果然現代瀏覽器强大無比,不吐槽舊瀏覽器了)
看看1000個數字
Array.prototype.unique1 = function(){
var obj = {},
ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(!obj[this[i]]){
obj[this[i]] = 1;
ary.push(this[i]);
}
}
return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique2 = function(){
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(ary.indexOf(this[i]) == -1) ary.push(this[i]);
}
return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique3 = function(){
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(this.indexOf(this[i]) == i) ary.push(this[i]);
}
return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique4 = function(){
this.sort(function(a,b){return a - b});
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(this[i] !== this[i-1]) ary.push(this[i]);
}
return ary;
}
function randomAry (n) {
var ary = [],i=0;
for(; i<n; i++){
ary.push(Math.ceil(Math.random()*10000));
}
console.log(ary)
return ary;
}
function useTime (fn) {
var start = new Date();
fn();
var end = new Date();
console.log('本次函數運行花了:' + (end - start) + '毫秒');
}
var ary = randomAry(1000),
fn1 = function(){
ary.unique1()
},
fn2 = function(){
ary.unique2()
},
fn3 = function(){
ary.unique3()
},
fn4 = function(){
ary.unique4()
};
useTime(fn1);
useTime(fn2);
useTime(fn3);
useTime(fn4);
方法一:1-2毫秒
方法二:40-50毫秒
方法三:40-50毫秒
方法四:0-1毫秒
看看10000個數字(數據龐大,開始吃不消了,等個五六秒吧)
Array.prototype.unique1 = function(){
var obj = {},
ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(!obj[this[i]]){
obj[this[i]] = 1;
ary.push(this[i]);
}
}
return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique2 = function(){
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(ary.indexOf(this[i]) == -1) ary.push(this[i]);
}
return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique3 = function(){
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(this.indexOf(this[i]) == i) ary.push(this[i]);
}
return ary.sort(function(a,b){return a - b});
}
Array.prototype.unique4 = function(){
this.sort(function(a,b){return a - b});
var ary = [],
i= 0,
len = this.length;
for(; i<len; i++){
if(this[i] !== this[i-1]) ary.push(this[i]);
}
return ary;
}
function randomAry (n) {
var ary = [],i=0;
for(; i<n; i++){
ary.push(Math.ceil(Math.random()*10000));
}
console.log(ary)
return ary;
}
function useTime (fn) {
var start = new Date();
fn();
var end = new Date();
console.log('本次函數運行花了:' + (end - start) + '毫秒');
}
var ary = randomAry(10000),
fn1 = function(){
ary.unique1()
},
fn2 = function(){
ary.unique2()
},
fn3 = function(){
ary.unique3()
},
fn4 = function(){
ary.unique4()
};
useTime(fn1);
useTime(fn2);
useTime(fn3);
useTime(fn4);
因爲數據龐大,我衹拿一次數據,你們喜歡就刷新幾次比較唄
方法一:10毫秒
方法二:1258毫秒
方法三:2972毫秒
方法四:5毫秒
原本還想給更大的數據看看,然後十萬個我的頁面就奔潰了。。。潰了。。。了
然後試試五萬個,也奔潰了。。。潰了。。。了
那就算吧,
反正在一萬個數據裏面對比,數據越大,方法一跟方法四按毫秒遞增,方法二跟方法三效率感人
效率計算:4>1>2>3
方法計算: 先排序,再比較前後兩個數字是不是相等 > 通過尋找對象屬性 > 通過尋找數組位置 > 搜索數字第一次出現的位置是不是跟當前位置一樣
看完上述内容,你们对使用JavaScript对数组去重的方法有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注天达云行业资讯频道,感谢大家的支持。