Summary about js array deduplication _ javascript tips-js tutorial

Source: Internet
Author: User
During project development, arrays often contain a lot of repeated content, that is, dirty data removal operations. This article focuses on several methods of array deduplication. For more information, see. 1.According to the principle that keys in js objects are not repeated, the method of removing duplicates from arrays is conceived. The most common thinking is as follows:

The Code is as follows:


Function distinctArray (arr ){
Var obj = {}, temp = [];
For (var I = 0; iif (! Obj [arr [I]) {
Temp. push (arr [I]);
Obj [arr [I] = true;
}
}
Return temp;
}
Var testarr = [1, 2, 3, 2];
Console. log (distinctArray (testarr); // [1, 2, 3]


It looks pretty good, but if it turns into the following:
Var testarr1 = [1, 2, 3, "2"];
Console. log (distinctArray (testarr); // [1, 2, 3]
The result is the same. This is not what we want. The result we need should be [1, 2, 3, "2"]. that is, the type integrity must be guaranteed during deduplication.

In response to the above situation, we have improved the above method:

The Code is as follows:


Function distinctArrayImprove (arr ){
Var obj = {}, temp = [];
For (var I = 0; iif (! Obj [typeof (arr [I]) + arr [I]) {
Temp. push (arr [I]);
Obj [typeof (arr [I]) + arr [I] = true;
}
}
Return temp;
}


The above method adds the typeof prefix to the key of the object, so let's see the effect.
Var testarr1 = [1, 2, 3, "2"];
Console. log (distinctArray (testarr); // [1, 2, 3, "2"]
Oh, good! So is this function completely OK? Let's look at another situation!
Var testarr1 = [1, 2, 3, "2", {a: 1}, {B: 1}];
Console. log (distinctArray (testarr); // [1, 2, 3, "2", {a: 1}]
How can I delete {B: 1} with this result? It is a serious problem if I accidentally delete useful data during deduplication, so the above method is not a type of perfect, so let's take a look.

2.In 1, our main idea is to use the idea that keys are not repeated in js objects to guide our thinking, but it does not solve all the problems in the end, then we can consider another way of thinking to implement the functions we want.

Use slice and splice methods to remove the array, as shown below:

The Code is as follows:


Function distinctArray2 (arr ){
Var temp = arr. slice (0); // copy an array to temp
For (var I = 0; I For (j = I + 1; j If (temp [j] = temp [I]) {
Temp. splice (j, 1); // Delete this element
J --;
}
}
}
Return temp;
}


Test:
Var testarr1 = [1, 2, 3, "2"];
Console. log (distinctArray (testarr); // [1, 2, 3]
Var testarr2 = [1, 2, {a: 1}, {a: 1}, {a: 1, B: 2}, function () {alert ("B") ;}, function () {alert ("B") ;}];
// [1, 2, {a: 1}, {a: 1}, {a: 1, B: 2}, function () {alert ("B ");}, function () {alert ("B");}]

The test results still cannot meet our needs. How can this problem be solved? After studying the above methods, we found that the main problem was to compare two operations with equal objects. In distinctArray2, we used "=" to compare them, it cannot tell whether the content of a large object is equal. In this case, we have written another method:

The Code is as follows:


Function distinctArrayAll (arr ){
Var isEqual = function (obj1, obj2 ){
// The two object addresses are equal and must be equal.
If (obj1 = obj2 ){
Return true;
}
If (typeof (obj1) = typeof (obj2 )){
If (typeof (obj1) = "object" & typeof (obj2) = "object "){
Var pcount = 0;
For (var p in obj1 ){
Pcount ++;
If (! IsEqual (obj1 [p], obj2 [p]) {
Return false;
}
}
For (var p in obj2 ){
Pcount --;
}
Return pcount = 0;
} Else if (typeof (obj1) = "function" & typeof (obj2) = "function "){
If (obj1.toString ()! = Obj2.toString ()){
Return false;
}
} Else {
If (obj1! = Obj2 ){
Return false;
}
}
} Else {
Return false;
}
Return true;
}
Var temp = arr. slice (0); // copy an array to temp
For (var I = 0; I For (j = I + 1; j If (isEqual (temp [j], temp [I]) {
Temp. splice (j, 1); // Delete this element
J --;
}
}
}
Return temp;
}


Test:
Var testArr3 = [1, 2, {a: 1}, {a: 1}, {a: 1, B: 2}, function () {alert ("B") ;}, function () {alert ("B") ;}];
Console. log (distinctArrayAll (testArr3 ));
// Result [1, 2, {a: 1}, {a: 1, B: 2}, function () {alert ("B");}]

Dear valued customer, the task has finally been successfully completed. As for the performance of each method, we will stay for the next discussion! We can see that the last method is the universal deduplication method, which can be used for complex arrays, but the corresponding execution overhead is also quite large, in actual project development, sometimes we only need to remove the duplicates of pure numbers or strings. This requires us to flexibly select the appropriate algorithms based on our needs, I only want to make the program more efficient on the basis of satisfying the requirements!

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.