Sometimes this kind of requirement is encountered, and you need to delete duplicate elements in the array and keep only one. The first method that comes to mind is probably to use 2 for loops to compare and remove duplicate elements. The code is as follows:
Method 1:
The code copy 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;
};
Usage Method 1 If you encounter more data, your performance will be much worse. Then please continue to see the following method.
Method 2:
The code copy 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 ); //Get index value
self.splice( n, 1 );
}
});
return self;
};
Method 2 uses the custom callback function of sort, and also uses indexOf, a method that is not supported by IE6/7/8. Of course, indexOf can be simulated by itself, but the bigger problem is that there is also a difference between the sort method of IE6/7/8 and the standard browser. There are many traps in custom callback functions that use the sort method in IE6/7/8. The code of the above custom sort callback function will directly report the "missing number" error in IE6/7/8. If the callback function returns NaN, this error will be reported, because in theory, the sort callback function can only return integers. Even if the return value is ignored, there are still other problems, and in the end, there is no too much trouble. Method 2 will not work in IE6/7/8.
From the Fool's Wharf, here is his code:
The code copy 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 the elements of the array. If you encounter duplicate array elements, it will be ignored. However, if you encounter the following array:
The code copy is as follows:
var arr = [ 'firefox', 1, '1' ];
If the above array is used in Method 3, it will mistakenly delete 1 and "1" as duplicate elements, so a little modification of Method 3 can be solved.
A modified version of Method 3:
The code copy 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 at the end of the article on Fools Wharf. This method is the same as the method provided by Rekey, but this method also has bugs. If you encounter such a 2B array, you will be fine:
The code copy is as follows:
var arr = [ 'firefox', 1, '1', 1 ];
The above array is modified with the method 3, and the last 3 elements will not be deleted. However, this kind of array is a bit extreme. If you encounter data with the same literal size and number of strings, you should be processed in advance to avoid this bug. The method of using temporary objects is slightly faster than sort in standard browsers, and the algorithms of the sort method should also be different in each browser.