table of Contents
- Analyzing deduplication indexOf
- After sorting to re-sort
- for + for + splice deduplication
- Array.form + Set to weight (used for ES6)
- [...new Set(arr)]
- Attribute can not use the same object
- 利用reduce+includes
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]
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
- js array deduplication five methods
- JavaScript array to heavy (12 kinds of methods, the most complete history)
- JavaScript array of deduplication in two ways -ES6
- ES6