Method 1 (indexOf()) :

  1. Create an empty array
  2. Iterate over the original array
  3. Use indexOf() to determine whether the indexOf the looping element equals the indexOf the element in the array
  4. If it is equal (which means it is a non-repeating element), the data is pushed into the new array
var arr1 = [1.3.5.7.9.1.3.5.7];
var arr2 = [];
for(var i=0; i<arr1.length; i++){
    // arr1.indexof (arr1[I]) == I checks the index position of the first occurrence of the element
    if(arr1.indexOf(arr1[i]) == i){
        arr2.push(arr1[i])
    }
}
console.log(arr2)  / /,3,5,7,9 [1]
Copy the code

IndexOf ()/splice()

  1. Through the array
  2. Use indexOf() to determine if the indexOf the looping element is not equal to the indexOf the element in the array
  3. If not, the element is removed from the array
  4. Delete the array length is shorter, so the index is also reduced by one
var arr1 = [1.3.5.7.9.1.3.5.7];
for(var i=0; i<arr1.length; i++){
    if(arr1.indexOf(arr1[i]) ! = i){ arr1.splice(i,1)
        // Since the array length is reduced by 1 each time the same element is deleted, the index of the array should also be reduced by 1i--; }}console.log(arr1)  / /,3,5,7,9 [1]
Copy the code

Splice () :

  1. Loop through the array twice
  2. Determine if each loop has the same value and different subscripts, and find the element that truncates the subscript position of the second array
  3. The original array becomes the new array with the new array removed
var arr1 = [1.3.5.7.9.1.3.5.7];
for(var i=0; i<arr1.length; i++){
    for(var j=0; j<arr1.length; j++){
        if(arr1[i]==arr1[j] && i! =j){// splice(j, 1) takes two arguments, the first argument is the index position to truncate the array, the second argument is the length to truncate the array
            arr1.splice(j, 1)}}}console.log(arr1)  / /,3,5,7,9 [1]
Copy the code

Method 4 (ES6) :

  1. The new Set() Set structure does not add duplicate values.
  2. Extended operators… Converted to an array
var arr1 = [1.3.5.7.9.1.3.5.7];
var arr2 = new Set(arr1);

console.log([...arr2])  / /,3,5,7,9 [1]
Copy the code

Method 5 (ES6) :

  1. The new Set() Set structure does not add duplicate values.
  2. Array.from() converts to an Array
var arr1 = [1.3.5.7.9.1.3.5.7];
var arr2 = new Set(arr1);

The array. from method converts a Set structure to an Array
console.log(Array.from(arr2))  / /,3,5,7,9 [1]
Copy the code

Method 6 (Reduce () method) :

  1. The new Set() Set structure does not add duplicate values.
  2. Array.from() converts to an Array
let arr = [1.1.2.3.4.5.5.6]
let arr2 = arr.reduce((ar,cur) = >{
  if(! ar.includes(cur)) { ar.push(cur) }return ar
},[])    / / [6]
Copy the code