Summary of js array deduplication

  • 2020-03-30 01:25:39
  • OfStack

1. According to the principle that the key in js object is not repeated, the method of array deduplication is conceived. The most conventional thinking is as follows:


function distinctArray(arr){
var obj={},temp=[];
for(var i=0;i<arr.length;i++){
if(!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 good, but here's what happens:
Var testarr1 = [1, 2, 3, "2");
The console. The log (distinctArray (testarr)); / / [1, 2, 3]
The result is still the same, which is not what we want, we need the result should be [1,2,3,"2"]. That is, the process of deduplication needs to ensure the integrity of the type.

In view of the above situations, we improve the above methods:


function distinctArrayImprove(arr){
var obj={},temp=[];
for(var i=0;i<arr.length;i++){
if(!obj[typeof (arr[i])+arr[i]]){
temp.push(arr[i]);
obj[typeof (arr[i])+arr[i]] =true;
}
}
return temp;
}

The above method prefixes the key with typeof, so let's take a look at the effect.
Var testarr1 = [1, 2, 3, "2");
The console. The log (distinctArray (testarr)); / / [1, 2, 3, "2"]
Oh, that's great! So is this function completely ok? Let's do one more case!
Var testarr1 = [1, 2, 3, "2", {a: 1}, {b: 1}].
The console. The log (distinctArray (testarr)); / / [1, 2, 3, "2", 1} {a:]
Unexpectedly appeared this result, how to delete {b:1} inexplicable, if there is a mistake in the process of deleting useful data is a very serious problem, so the above method is not a perfect one, then let us go on to see.

2. In 1, our main idea is to use the idea that the key in js object does not repeat to guide our thinking, but it does not solve all the problems in the end, then we can consider a new thinking mode to achieve the function we want.

Slice and splice are used to deduplicate the array, as follows:


function distinctArray2(arr){
var temp=arr.slice(0);//Make a copy of the array to temp
for(var i=0;i<temp.length;i++){
for(j=i+1;j<temp.length;j++){
if(temp[j]==temp[i]){
temp.splice(j,1);//Delete the element
j--;
}
}
}
return temp;
}

Testing:
Var testarr1 = [1, 2, 3, "2");
The console. The log (distinctArray (testarr)); / / [1, 2, 3]
Var testarr2 = [1,2,2, {a: 1}, {a: 1}, {a: 1, b: 2}, function () {alert (" b ");}, function () {alert (" b ");}].
A: / / [1, 2, {1}, {a: 1}, {a: 1, b: 2}, function () {alert (" b ");}, function () {alert (" b ");}]

The test results still cannot meet our requirements, what should we do? After our study of the above methods, we found that the main issue was the operation to compare two objects equal. The use of "==" in distinctArray2 does not distinguish between the contents of large objects equal. In view of this, we wrote a separate method:


function distinctArrayAll(arr){
var isEqual=function(obj1,obj2){
//If two object addresses are equal, they 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);//Make a copy of the array to temp
for(var i=0;i<temp.length;i++){
for(j=i+1;j<temp.length;j++){
if(isEqual(temp[j],temp[i])){
temp.splice(j,1);//Delete the element
j--;
}
}
}
return temp;
}

Testing:
Var testArr3 = [1,2,2, {a: 1}, {a: 1}, {a: 1, b: 2}, function () {alert (" b ");}, function () {alert (" b ");}].
The console. The log (distinctArrayAll (testArr3));
/ / the results of [1, 2, 1} {a:, 2} {a: 1, b:, function () {alert (" b ");}]

Alas, the deduplication task has finally been successfully completed, and we'll discuss the performance of each method next time! We can see the last way is universal to heavy method, can according to complex array and heavy, but the corresponding execution cost is quite large, sometimes we need in the actual project development may simply be pure Numbers or strings to heavy, this requires we choose corresponding algorithm according to requirements, not too perfect, on the basis of the demand for make the program more efficient!


Related articles: