This paper introduces the eight – point array deduplication method.

ES6 Set (most commonly used in ES6)

Function unique(arr) {return Array. From ([...new Set(arr)]); } // Method 2:  function unique(arr) { return [...new Set(arr)] } var arr = [ 'true', 'true', true, true, undefined, undefined, null, null, NaN, NaN, 0, 0, 'a', 'a', {}, {} ]; console.log(unique(arr)); // ["true", true, undefined, null, NaN, 0, "a", {}, {}]Copy the code

2. Map data structure is used for deduplication

Function unique(arr) {const newArray = []; const tmp = new Map(); for(let i = 0; i < arr.length; i++) { if (! tmp.has(arr[i])) { tmp.set(arr[i], 1) newArray.push(arr[i]) } } return newArray; } function unique(arr) {const TMP = new Map(); return arr.filter(item => ! tmp.has(item) && tmp.set(item, 1)); } var arr = [ 'true', 'true', true, true, undefined, undefined, null, null, NaN, NaN, 0, 0, 'a', 'a', {}, {} ]; console.log(unique(arr)); / / [" true ", true, undefined, null, NaN, 0, "a", {}, {}] / / {} not to heavyCopy the code

3, use “for” to nest “for”, and then splice (ES5 most commonly used)

function unique(arr){ for(let i = 0; i < arr.length; i++){ for(let j = i+1; j < arr.length; J++) {if (arr [I] = = arr [j]) {/ / find two identical elements, delete a arr. Splice (j, 1); j--; } } } return arr; } var arr = [ 'true', 'true', true, true, undefined, undefined, null, null, NaN, NaN, 0, 0, 'a', 'a', {}, {} ]; console.log(unique(arr)); // ["true", "true", undefined, NaN, NaN, 0, "a", {}, {}] // NaN and {} are not deleted because null is the same as undefinedCopy the code

4. Use indexOf to remove weight

function unique(arr) { const resArr = []; for (let i = 0; i < arr.length; i++) { if (resArr.indexOf(arr[i]) === -1) { resArr.push(arr[i]) } } return resArr; } var arr = [ 'true', 'true', true, true, undefined, undefined, null, null, NaN, NaN, 0, 0, 'a', 'a', {}, {} ]; console.log(unique(arr)); / / [" true ", true, undefined, null, NaN, NaN, 0, "a", {}, {}] / / NaN, {} not to heavyCopy the code

V. Use sort

function unique(arr) { let newArr = arr.sort(); let array = [newArr[0]]; for (let i = 1; i < newArr.length; i++) { if (newArr[i] ! == newArr[i-1]) { array.push(newArr[i]); } } return array; } var arr = [ 'true', 'true', true, true, undefined, undefined, null, null, NaN, NaN, 0, 0, 'a', 'a', {}, {} ]; console.log(unique(arr)); / / [" true ", true, undefined, null, NaN, NaN, 0, "a", {}, {}] / / NaN, {} not to heavyCopy the code

Use the uniqueness of object attributes

function unique(arr) { let array = []; let obj = {}; for (let i = 0; i < arr.length; i++) { if (! Obj [arr[I]]) {// Arr [I] is true. obj[arr[i]] = 1; } else { obj[arr[i]]++; } } return array; } var arr = [ 'true', 'true', true, true, undefined, undefined, null, null, NaN, NaN, 0, 0, 'a', 'a', {}, {} ]; console.log(unique(arr)); / / / "true", undefined, null, NaN, NaN, 0, "a", {}, {}] / / two true directly removed, NaN, {} did not goCopy the code

7. Use includes

function unique(arr) { const array = []; for(var i = 0; i < arr.length; I++) {//includes checks whether the array has a value if(! array.includes(arr[i])) { array.push(arr[i]); } } return array; } var arr = [ 'true', 'true', true, true, undefined, undefined, null, null, NaN, NaN, 0, 0, 'a', 'a', {}, {} ]; console.log(unique(arr)); / / [" true ", true, undefined, null, NaN, 0, "a", {}, {}] / / {} not to heavyCopy the code

8. Use reduce+includes

function unique(arr){ return arr.reduce((prev,cur) => prev.includes(cur) ? prev : [...prev,cur], []); } var arr = [ 'true', 'true', true, true, undefined, undefined, null, null, NaN, NaN, 0, 0, 'a', 'a', {}, {} ]; console.log(unique(arr)); / / [" true ", true, undefined, null, NaN, 0, "a", {}, {}] / / {} not to heavyCopy the code

conclusion

Set, Map, indexOf, includes, filter, sort, double for loop, and uniqueness of object attributes are introduced for array de-duplication. In contrast, Set and Map data structures have better performance in de-duplication.

Refer to the article

JavaScript array deduplication

Unlock multiple JavaScript arrays to go heavy poses