js--array de-weight 3 ways

Source: Internet
Author: User

Summary of three common methods of JS array de-weight

The first is a more conventional approach.

Ideas:

1. Build a new array to store the results

Each time an element is removed from the original array in a 2.for loop, the loop is compared with the result array.

3. If the element is not in the result array, it is stored in the result array

Copy the code code as follows:
Array.prototype.unique1 = function () {
var res = [This[0]];
for (var i = 1; i < this.length; i++) {
var repeat = false;
for (var j = 0; J < Res.length; J + +) {
if (this[i] = = Res[j]) {
repeat = true;
Break
}
}
if (!repeat) {
Res.push (This[i]);
}
}
return res;
}
var arr = [1, ' A ', ' a ', ' B ', ' d ', ' e ', ' e ', 1, 0]
Alert (arr.unique1 ());


The second method is more efficient than the above method

Ideas:

1. Sort the original array first

2. Check that the first element in the original array is the same as the last element in the result array, because it is already sorted, so the repeating element is in the adjacent position

3. If it is not the same, the element is stored in the result array

Copy the code code as follows:
Array.prototype.unique2 = function () {
This.sort (); Sort first
var res = [This[0]];
for (var i = 1; i < this.length; i++) {
if (This[i]!== res[res.length-1]) {
Res.push (This[i]);
}
}
return res;
}
var arr = [1, ' A ', ' a ', ' B ', ' d ', ' e ', ' e ', 1, 0]
Alert (Arr.unique2 ());


The second method also has some limitations because it is sorted before going to heavy, so the final return of the de-weight result is also sorted. This method is not available if the order of the array is not changed to be heavy.

Third method (recommended)

Ideas:

1. Create a new array to hold the result

2. Create an empty object

3.for Loop, each time an element is taken out and compared to the object, if the element is not duplicated, it is stored in the result array, and the content of the element as an object property, and assigned a value of 1, deposited in the 2nd step of the object created.

Note: As for how to compare, it is to remove an element from the original array, and then go to the object to access the property, if you can access the value, then the description is repeated.

Copy the code code as follows:
Array.prototype.unique3 = function () {
var res = [];
var json = {};
for (var i = 0; i < this.length; i++) {
if (!json[this[i]]) {
Res.push (This[i]);
Json[this[i]] = 1;
}
}
return res;
}

var arr = [112,112, 34, ' Hello ', 112,112, 34, ' hello ', ' str ', ' str1 '];
Alert (Arr.unique3 ());

js--array de-weight 3 ways

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.