JS Array to weight comparison

Source: Internet
Author: User
Tags filter array comparison return

An array to repeat is a common requirement, and we temporarily consider the same type of array to repeat. The main reason is to clear the thinking and consider performance.
The For loop deletes the following duplicate

var uniquefor = function (arr) {
for (var i = 0; i < arr.length-1; i++) {
var item = Arr[i];
for (var j = i+1 J < Arr.length; J + +) {
item = = = Arr[j] && (Arr.splice (J, 1), j--);
}
};
return arr;
};

Determining Object Properties

var uniqueobject = function (arr) {
var V, r = [], o = {};
for (var i = 0; (v = arr[i])!== undefined; i++) {
!O[V] && (R.push (v), o[v] = true);
}
return R;
};

Array Filter Duplicates Filter

var uniquefilter = function (arr) {
Return Arr.filter (Function (Elem, POS, self) {
Returns true if there are no duplicates
Return Self.indexof (Elem, pos + 1) = =-1;
});
};

Compare execution efficiency

Figure 1 Chrome browser

Figure 2 Firefox browser



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.