JavaScript deduplication

table of Contents

  1. Analyzing deduplication indexOf
  2. After sorting to re-sort
  3. for + for + splice deduplication
  4. Array.form + Set to weight (used for ES6)
  5. [...new Set(arr)]
  6. Attribute can not use the same object
  7. 利用reduce+includes

1, for loop + indexOf

1.1 array traversal - the most intuitive and best understood

Create a new array newArr, arr traversal, the use of indexOf determine whether the current numbers already exist in the new array does not exist to put such newArr, if there is a deal not continue traversal.

var arr = [2, 8, 5, 0, 5, 2, 6, 7, 2]
  var newArr = []
  for (var i = 0; i < arr.length; i++) {
    if (newArr.indexOf(arr[i]) === -1) {
      newArr.push(arr[i])
    }
  }
  console.log(newArr) // 结果:[2, 8, 5, 0, 6, 7]

1.2 Analyzing array subscript

This method is a method similar to the first, or the use of indexOf , the difference is using indexOf position to return first appears.

var arr = [2, 8, 5, 0, 5, 2, 6, 7, 2]
var newArr = []
for (var i = 0; i < arr.length; i++) {
    if (arr.indexOf(arr[i]) === i) {
        newArr.push(arr[i])
    }
}
console.log(newArr) // 结果:[2, 8, 5, 0, 6, 7]

Similar to the above, you may also be used Includes .

var ARR = [2,. 8,. 5, 0,. 5, 2,. 6,. 7, 2 ]
 var newArr = [];
 for ( var I = 0; I <arr.length; I ++ ) {
     IF (newArr.includes! (ARR [I])) { // if the detector array has a value includes 
        newArr.push (ARR [I]); 
    } 
} 
the console.log (newArr) // results: [2, 8, 5, 0, 6 , 7]

 

 

2, Sort sorted deduplication

We used the sort order. After ordering, duplicate numbers close together, before and after, the new array into different newArr inside.

var arr = [2, 8, 5, 0, 5, 2, 6, 7, 2]
arr.sort()
var newArr = [arr[0]]
for (var i = 1; i < arr.length; i++) {
    if (arr[i] !== newArr[newArr.length - 1]) {
        newArr.push(arr[i])
    }
}
console.log(newArr) // 结果:[0, 2, 5, 6, 7, 8]

 

. 3, for + + for splice deduplication

For double-nested loop + splice delete a duplicate.

var ARR = [2,. 8,. 5, 0,. 5, 2,. 6,. 7, 2 ]
 for ( var I = 0; I <arr.length; I ++ ) {
     for ( var J = I +. 1; J <ARR .length; J ++ ) {
         IF (ARR [I] == ARR [J]) { // first equivalent to a second, splice delete the second method 
            arr.splice (J,. 1 ); 
            J - ; 
        } 
    } 
} 
the console.log (ARR) // results: [2, 8, 5, 0, 6, 7]

 

. 4, Array.form + the Set to weight (used for ES6)

ES6 method, less code, easy to use, a disadvantage does not recognize the object.

var arr = [2, 8, 5, 0, 5, 2, 6, 7, 2]
let newArr = Array.from(new Set(arr))
console.log(newArr) // 结果:[2, 8, 5, 0, 6, 7]

 

5、[...new Set(arr)]

Simplified fourth method.

var arr = [2, 8, 5, 0, 5, 2, 6, 7, 2]
let newArr = [...new Set(arr)] 
console.log(newArr) // 结果:[0, 2, 5, 6, 7, 8]

 

6, using the object attribute is not the same

 Defines an object obj, obj is the key to arr determines whether key already exists.

var ARR = [2,. 8,. 5, 0,. 5, 2,. 6,. 7, 2 ]
 var newArr = [];
 var obj = {}
 for ( var I = 0; I <arr.length; I ++ ) {
     IF {(obj [arr [I]]!) // to the value of obj arr Key 
        newArr.push (arr [I]); 
        obj [arr [I]] =. 1 
    } 
} 
the console.log (newArr) // results: [2, 8, 5, 0, 6, 7]

 7、利用reduce+includes

Use reduce the accumulator, the principle is similar for + includes.

var arr = [2, 8, 5, 0, 5, 2, 6, 7, 2]
var newArr = arr.reduce((prev,cur) => prev.includes(cur) ? prev : [...prev,cur],[]);
console.log(newArr) // 结果:[2, 8, 5, 0, 6, 7]

 


reference

  1. js array deduplication five methods
  2. JavaScript array to heavy (12 kinds of methods, the most complete history)
  3. JavaScript array of deduplication in two ways -ES6
  4. ES6

 

Guess you like

Origin www.cnblogs.com/Nightsky-Dec/p/11374886.html