Home  >  Article  >  How to remove duplicates from js array

How to remove duplicates from js array

百草
百草Original
2023-07-05 11:32:401674browse

JS array deduplication method: 1. "new Set()" method provided by ES6; 2. "filter()" method; 3. Use for loop with "indexOf()" method; 4. , compare each element of the array with other elements in turn, and delete duplicate elements; 5. Create a new empty array, use the "indexOf()" method to determine the index of the current element in the array, if it is the same as the subscript of the loop is added to the new array; 6. Double for loop; 7. "includes()" method.

How to remove duplicates from js array

The operating system of this tutorial: windows10 system, JavaScript ECMAScript 2023 version, DELL G3 computer.

1. Use the Set structure new Set() provided by ES6. It is easy to use and highly recommended.

Directly add it to a new array and use 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];
 
  console.log(arr);   
 
  function noRepeat(arr){
 
    var newArr = [...new Set(arr)]; //利用了Set结构不能接收重复数据的特点
 
    return newArr
 
  }
 
  var arr2 = noRepeat(arr)
 
  console.log(arr2);

2. Use filter() to remove duplicates

The filter() method creates a new array. The elements in the new array are checked for all elements in the specified array that meet the conditions. item is the value of the current element, and index is the index value of the current element. The indexOf() method returns the first occurrence of a specified string value in a string. Use indexOf() to query the subscript of the array to see if it is equal to the current subscript. If equal, return it, otherwise no value will be returned.

var arr = ['apple','apps','pear','apple','orange','apps'];
 
console.log(arr)    
  var newArr = arr.filter(function(item,index){
     return arr.indexOf(item) === index;  // 因为indexOf 只能查找到第一个  
  });
 
console.log(newArr);

3. Use for loop with indexOf to remove duplicates

 var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22];
 function noRepeat(arr) {
//定义一个新的临时数组 
var newArr=[]; 
//遍历当前数组 
for(var i=0;i<arr.length;i++) {
  //如果当前数组的第i已经保存进了临时数组,那么跳过,
  //否则把当前项push到临时数组里面 
  if(newArr.indexOf(arr[i]) === -1) {  //indexOf() 判断数组中有没有字符串值,如果没有则返回 -1 
     newArr.push(arr[i]);
  }
    }
    return newArr
  }
  var arr2 = noRepeat(arr);
  console.log(arr2);

4. Compare each element of the array with other elements in turn and find duplicate elements. Deletion is cumbersome and not recommended

 var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22];
 console.log(arr);    
 function noRepeat(arr) {
        for(var i = 0; i < arr.length-1; i++){
            for(var j = i+1; j < arr.length; j++){
                if(arr[i]===arr[j]){
                    arr.splice(j,1);
                    j--;
                }
            }
        }
        return arr;
 }
 var arr2 = noRepeat(arr);
 console.log(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, add it to the new array

var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22];
    console.log(arr)    
    function noRepeat(arr) {
        var newArr = [];
        for (var i = 0; i < arr.length; i++) {
            if (arr.indexOf(arr[i]) == i) {
              newArr.push(arr[i]);
            }
        }
        return newArr;
    }
   var arr2 = noRepeat(arr);
   console.log(arr2);

6. Use a double for loop

var arr = [1,9,8,8,7,2,5,3,3,3,2,3,1,4,5,444,55,22];
console.log(arr);    
function noRepeat(arr){
   for (var i = 0; i < arr.length; i++) {
       for (var j = 0; j < arr.length; j++) {
           if (arr[i] == arr[j] && i != j) { //将后面重复的数删掉
              arr.splice(j, 1);
            }
       }
    }
    return arr;
}
var arr2  = noRepeat(arr);
console.log(arr2);

7. Use includes 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<arr.length; i++){
        if(!newArr.includes(arr[i])){
            newArr.push(arr[i])
        }
      }
     return newArr
   }
 console.log(noRepeat(arr));

The above is the detailed content of How to remove duplicates from js array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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