1. With the help of the Set structure provided by ES6, new Set() is simple and easy to use. Highly recommended
Just give it a new array, using the extension operator of es6
var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22]; (arr); function noRepeat(arr){ var newArr = [...new Set(arr)]; //Use the feature that the Set structure cannot receive duplicate data return newArr } var arr2 = noRepeat(arr) (arr2);
2. Use filter() to deduplicate
The filter() method creates a new array, and the elements in the new array are checked for all elements in the specified array that meet the criteria. item is the value of the current element, and index is the index of the current element. The indexOf() method returns the first occurrence of a specified string value in the string. Use indexOf() to query the subscript of the array to see if it is equal to the current subscript. If it is equal, it will be returned, otherwise the value will not be returned.
var arr = ['apple','apps','pear','apple','orange','apps']; (arr) var newArr = (function(item,index){ return (item) === index; // Because indexOf can only find the first one }); (newArr);
3. Use for loop to match indexOf to remove reload
var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22]; function noRepeat(arr) { //Define a new temporary array var newArr=[]; //Transfer the current array for(var i=0;i<;i++) { //If the i-th of the current array has been saved into the temporary array, then skip it. // Otherwise push the current item into a temporary array if((arr[i]) === -1) { //indexOf() determines whether there is a string value in the array. If not, return -1 (arr[i]); } } return newArr } var arr2 = noRepeat(arr); (arr2);
4. Compare each element of the array with other elements in sequence, find duplicate elements, delete them. It is more cumbersome. Not recommended.
var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22]; (arr); function noRepeat(arr) { for(var i = 0; i < -1; i++){ for(var j = i+1; j < ; j++){ if(arr[i]===arr[j]){ (j,1); j--; } } } return arr; } var arr2 = noRepeat(arr); (arr2);
5. Use the new array to determine the index of the current element in the array through the indexOf method. If it is equal to the subscript of the loop, it will be added to the new array.
var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22]; (arr) function noRepeat(arr) { var newArr = []; for (var i = 0; i < ; i++) { if ((arr[i]) == i) { (arr[i]); } } return newArr; } var arr2 = noRepeat(arr); (arr2);
6. Use double for loops
var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22]; (arr); function noRepeat(arr){ for (var i = 0; i < ; i++) { for (var j = 0; j < ; j++) { if (arr[i] == arr[j] && i != j) { //Delete the number after the repeated number (j, 1); } } } return arr; } var arr2 = noRepeat(arr); (arr2);
7. Use include to implement array deduplication
var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22]; function noRepeat(arr) { let newArr = []; for(i=0; i<; i++){ if(!(arr[i])){ (arr[i]) } } return newArr } (noRepeat(arr));
The above are the seven commonly used methods
Summarize
This is the end of this article about 7 common methods for deduplication of js arrays. For more related contents of js arrays, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!