This article mainly introduces the ultimate Summary of javascript Array deduplication. This article lists three methods of javascript Array deduplication, and analyzes their advantages and disadvantages, if you need it, you can refer to this requirement. You need to delete the repeated elements in the array and keep only one element. The first thought is probably to use two for loops for comparison and then remove the repeated elements. The Code is as follows:
Method 1:
The Code is as follows:
Array. prototype. distinct = function (){
Var arr = [],
Len = this. length;
For (var I = 0; I <len; I ++ ){
For (var j = I + 1; j <len; j ++ ){
If (this [I] === this [j]) {
J = ++ I;
}
}
Arr. push (this [I]);
}
Return arr;
};
Method 1: if there are too many data, the performance will be much worse. Proceed to the following method.
Method 2:
The Code is as follows:
Array. prototype. distinct = function (){
Var self = this,
Arr = self. concat (). sort (); // create a new array and sort it.
Arr. sort (function (a, B ){
If (a = B ){
Var n = self. indexOf (a); // obtain the index value
Self. splice (n, 1 );
}
});
Return self;
};
Method 2 uses the sort custom callback function and the indexOf method not supported by IE6/7/8. Of course, indexOf can be simulated by yourself, but the bigger problem is that the sort method of IE6/7/8 is also different from the standard browser. In IE6/7/8, the sort method has many custom callback function traps, in IE6/7/8, the code of the callback function of the preceding custom sort directly reports a "number missing" error. If the callback function returns NaN, this error is reported, in theory, the sort callback function can only return integers. Even if the problem of ignoring the return value still has other problems, there is not much tangle in the end, and method 2 does not work in IE6/7/8.
From the perspective of yuren Wharf, method 3 is shown in the following code:
The Code is as follows:
Array. prototype. delRepeat = function (){
Var newArray = [];
Var provisionalTable = {};
For (var I = 0, item; (item = this [I])! = Null; I ++ ){
If (! ProvisionalTable [item]) {
NewArray. push (item );
ProvisionalTable [item] = true;
}
}
Return newArray;
};
Method 3 uses a temporary object to store array elements. If repeated array elements are encountered, they will be ignored. However, if you encounter the following array:
The Code is as follows:
Var arr = ['Firefox ', 1, '1'];
If method 3 is used for the preceding array, the values 1 and 1 are mistakenly deleted as duplicate elements. Therefore, method 3 is slightly modified to solve this BUG.
Modified version of method 3:
The Code is as follows:
Array. prototype. distinct = function (){
Var arr = [],
Obj = {},
I = 0,
Len = this. length,
Result;
For (; I <len; I ++ ){
Result = this [I];
If (obj [result]! = Result ){
Arr. push (result );
Obj [result] = result;
}
}
Return arr;
};
Later, I read the comments behind the Yuen terminal article. This method is the same as the method provided by Rekey, but there are bugs in this method. If such an array of 2B is met, I will get a cup of cake:
The Code is as follows:
Var arr = ['Firefox ', 1, '1', 1];
The modified version of method 3 for the preceding array will not delete the last three elements, but this array is a bit extreme, if you encounter data with the same string literal volume and number, you should handle it in advance to avoid this BUG. The method of using temporary objects is slightly faster than that of sort in the standard browser. The algorithm of sort in different browsers should also be different.