Summary of JS array deduplication methods

小云云
Release: 2018-03-17 16:36:02
Original
1611 people have browsed it

This article mainly shares with you a summary of JS array deduplication methods. There are seven methods in total. I hope it can help everyone.

The easiest way:

##?

1

2

3

4

##5

6

7

8

9

10

11

12

13

14

15

16

17

18


var arr=[2,8,5,0,5,2,6,7,2];
function unique1(arr){
var hash=[];
for (var i = 0; i < arr.length; i++) {
if(hash.indexOf(arr[i])==-1){
hash.push(arr[i]);
}
}
return hash;

}


Method 1:

Double-layer loop, outer loop element, inner loop comparison value

If there is If the values are the same, they will be skipped. If they are not the same, they will be pushed into the array.

?

Method 2: Use splice to operate directly on the original array

Double-layer loop, outer loop element, inner loop comparison value

When the values are the same, delete this value

Note: After deleting the element, you need to reduce the length of the array by 1.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18


Array.prototype.distinct =function(){

vararr =this,

result = [],

i,

j,

len = arr.length;

for(i = 0; i < len; i++){

for(j = i + 1; j < len; j++){

if(arr[i] === arr[j]){

j = ++i;

}

}

result.push(arr[i]);

}

returnresult;

##}

vararra = [1,2,3,4,4,1,1,2,1,1,1];

arra.distinct();//Return [3,4,2,1]

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19


Array.prototype.distinct =function(){

vararr =this,

i,

j,

len = arr.length;

for(i = 0; i < len; i++){

for(j = i + 1; j < len; j++){

if(arr[i] == arr[j]){

arr.splice(j,1);

len--;

j--;

#}

#}

}

returnarr;

};

vara = [1,2,3,4,5,6,5,3,2,4,56,4,1,2,1,1,1,1,1,1,];

varb = a.distinct();

console.log(b.toString());//1,2,3,4,5,6,56

Advantages: simple and easy to understand

Disadvantages: high memory usage and slow speed

Method 3: Use the properties of objects that cannot be the same to deduplicate

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17


Array.prototype.distinct =function(){

vararr =this,

i,

obj = {},

result = [],

len = arr.length;

for(i = 0; i< arr.length; i++){

if(!obj[arr[i]]){//If it can be found, it proves that the array elements are repeated

obj[arr[i]] = 1;

result.push(arr[ i]);

}

}

returnresult;

};

vara = [1,2,3,4,5,6,5,3,2,4,56,4,1,2,1,1,1,1,1,1,];

varb = a.distinct();

console.log(b.toString());//1,2,3,4,5,6,56

Method 4: Recursive deduplication of arrays

Use the recursive idea

Sort first, and then compare from the end, if you encounter the same , then delete


?

##1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20


Array.prototype.distinct =function(){

vararr =this,

len = arr.length;

arr.sort(function(a,b){//Sort the array to facilitate comparison

returna - b;

})

functionloop(index){

if(index >= 1){

if(arr[index] === arr[index-1]){

arr.splice(index,1);

}

loop(index - 1);//Recursive loop function to remove duplicates

}

}

loop(len-1);

returnarr;

};

vara = [1,2,3,4,5,6,5,3,2,4,56,4,1,2,1,1,1,1,1,1,56,45,56];

varb = a.distinct();

console.log(b.toString());//1,2,3,4,5,6,45,56

Method Five: Using indexOf and forEach

?

##1

2

3

4

5

6

7

8

9

10

11

12

13

14

15


Array.prototype.distinct =function(){

vararr =this,

result = [],

len = arr.length;

arr.forEach(function(v, i ,arr){//Use map and filter here The method can also be implemented

varbool = arr.indexOf(v,i+1);// Start looking for duplicates from the next index value of the incoming parameter

if(bool === -1){

result.push(v);

}

})

returnresult;

};

vara = [1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,3,2,3,3,2,2,1,23,1,23,2,3,2,3,2,3];

varb = a.distinct();

console.log(b.toString());//1,23,2,3

Method 6: Use ES6's set

Set data structure, which is similar to an array, and the values of its members are all unique.

Use Array.from to convert the Set structure into an array

?

##1

2

3

4


functiondedupe(array){

returnArray.from(newSet(array));

}

dedupe([1,1,2,3])//[1,2,3]

#

The expansion operator (...) uses the for...of loop internally

?

1

2

3


#let let arr = [1,2,3,3 ];

let resultarr = [...newSet(arr)];

console.log(resultarr);//[1,2,3]

The following is a supplementary introduction to the method of merging arrays and removing duplicates

##1. concat() method

Idea: The concat() method combines the incoming array or non-array value with the original array to form a new array and returns it. This method will generate a new array.


##1

2

3

4

5

functionconcatArr(arr1, arr2){

##vararr = arr1.concat (arr2);

arr = unique1(arr);//Refer to any of the above deduplication methods

returnarr;

}

2. Array.prototype.push.apply()

Idea: The advantage of this method is that it does not generate a new array.

?

##1

2

3

4

5

6

7

8

9

10


vara = [1, 2, 3];

varb = [4, 5, 6];

Array.prototype.push.apply(a, b);//a=[1,2,3,4,5,6]

//等效于:a.push.apply(a, b);

//Also equivalent to [].push.apply(a, b);

functionconcatArray(arr1,arr2){

Array.prototype.push.apply(arr1, arr2);

arr1 = unique1(arr1);

returnarr1;

##Related recommendations:

Detailed explanation of array deduplication examples in js

Six ways to deduplicate JS arrays

How to deduplicate JavaScript arrays Ways to share

The above is the detailed content of Summary of JS array deduplication methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!