JavaScript Array method of the ultimate summary _javascript skills

Source: Internet
Author: User
Tags javascript array

Sometimes this needs to be removed from the duplicated elements of the array, leaving only one. The first way to think of it is probably to use 2 for loops to compare and then remove the duplicate elements, as shown in the following code:

Method 1:

Copy Code code 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;
};

The use of Method 1 would be much worse if the data were to be encountered more often. Then keep looking at the following method.

Method 2:

Copy Code code as follows:

Array.prototype.distinct = function () {

var self = this,
arr = Self.concat (). sort (); Create a new array and sort

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 for sort, and also uses a method that indexOf this IE6/7/8 does not support. Of course, indexof can simulate itself, but the bigger problem is that there is a difference between the Ie6/7/8 sort method and the standard browser. There are more custom callback function traps using the Sort method in Ie6/7/8, and the code in the custom sort callback function above will report the "missing numbers" error directly in IE6/7/8, and the callback function will return Nan if it's returned. Because theoretically sort callback function can only return an integer. Even if you ignore the return value of the problem there are other problems, and finally there is not too much to tangle, Method 2 in the IE6/7/8 is not workable.

From the pier of fools it looks like Method 3, here's his code:

Copy Code code 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 an array, which is ignored if a duplicate array element is encountered. However, if you encounter the following array:

Copy Code code as follows:

var arr = [' Firefox ', 1, ' 1 '];

If the above array uses method 3 to mistakenly delete 1 and "1" as a repeating element, then a little modification of Method 3 can solve the bug.
Modified version of Method 3:

Copy Code code 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;
};

And then read the comments at the end of the stupid dock article, the method is the same as the one provided by rekey, but this method also has bugs, if you encounter such a 2B array on the Cup:

Copy Code code as follows:

var arr = [' Firefox ', 1, ' 1 ', 1];

The above array, with the modified version of Method 3, will not delete the following 3 elements, but this array is a bit extreme, and if you encounter string literals and numbers the same data should be processed in advance to circumvent the bug. The method of using a temporary object is slightly faster than sort in a standard browser, and the sort method should have a different algorithm in each browser.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.