JS array to heavy is front-end interview, often investigate a problem, you will several methods? If you have a better idea, provide it in the comments below. Let’s look at eight array de-duplicity methods, each in a block-level scope, with methods and test cases. So you can better test and understand.

Method 1 For nested For uses splice to rewrite the array forward traversal loop

Decrement by 1 if the array is deleted

{
    let arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.'NaN'.0.0.'a'.'a', {}, {}];function distinct (arr) {
        for(let i = 0; i < arr.length; i++) {
            for(let j = i + 1; j < arr.length ; j++) {
                if(arr[i] === arr[j]) {
                    arr.splice(j, 1)
                    j--;
                }
            }
        }
    } 
    
    distinct(arr)
    console.log(arr) / / (1, "true", true, 15, false, undefined, null, NaN, NaN, "NaN," 0, "a", {...}, {...}]
}
Copy the code
  • Advantages: This method allows for duplicationString,Boolean,Number,undefined,null, returns the original array after the deduplication.
  • Disadvantages: Cannot be filtered outNaN,Object

Method 2 For nested For uses splice to reverse the loop by rewriting the array

Reverse loop traversal encountered repeated directly delete

{
    let arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.'NaN'.0.0.'a'.'a', {}, {}];function distinct (arr) {
        for(let i = arr.length; i > 0; i--) {
            for(let j = i - 1; j > - 1 ; j--) {
                if(arr[i] === arr[j]) {
                    console.log(arr[j])
                    arr.splice(j, 1)
                }
            }
        }
    } 
    
    distinct(arr)
    console.log(arr) / / (1, "true", true, 15, false, null, NaN, NaN, "NaN," 0, "a", {...}, {...}]
}
Copy the code

The advantages and disadvantages of the same method 1:

  • Advantages: This method allows for duplicationString,Boolean,Number,undefined, ‘null’, returns the original array after the deduplication.
  • Disadvantages: Cannot be filtered outNaN,Object

Method 3: includes returns the new array

{
    let arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.'NaN'.0.0.'a'.'a', {}, {}];function distinct (arr) {
        let newArr = []
        for(let i = 0; i < arr.length; i++) {
            if(! newArr.includes(arr[i])) { newArr.push(arr[i]) } }return newArr
    }
    
    console.log(distinct(arr)) / / (1, "true", true, 15, false, undefined, null, NaN, "NaN," 0, "a", {...}, {...}]
}
Copy the code

The advantage is that repeated nans can be filtered, but a new array is returned, which consumes more storage space than method 1 or 2.

  • Advantages: This method allows for duplicationString,Boolean,Number,undefined,null,NaN, returns the new array with the rehash removed.
  • Disadvantages: Cannot be filtered outObject

Method 4 indexOf to return the new array

{
    let arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.'NaN'.0.0.'a'.'a', {}, {}];function distinct (arr) {
        let newArr = []
        for(let i = 0; i < arr.length; i++) {
            if(newArr.indexOf(arr[i]) < 0) {
                newArr.push(arr[i])
            } 
        }
        return newArr
    }
    
    console.log(distinct(arr)) / / (1, "true", true, 15, false, undefined, null, NaN, NaN, "NaN," 0, "a", {...}, {...}]
}
Copy the code

This method is similar to includes and does not filter out nans.

  • Advantages: This method allows for duplicationString,Boolean,Number,undefined,null, returns the new array with the rehash removed.
  • Disadvantages: Cannot be filtered outNaN,Object

Method 5 uses the unique property of the object’s attribute key to remove the weight

{
    let arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.'NaN'.0.0.'a'.'a', {}, {}];function distinct(arr) {
        let obj = {}
        let newArr = []
        for(let i = 0; i < arr.length; i++) {
            if(! obj[arr[i]]){ obj[arr[i]] =1
                newArr.push(arr[i])
            }
        }
        return newArr
    }

    console.log(distinct(arr)) / / (1, "true", 15, false, undefined, null, NaN, 0, "a", {...}]
}
Copy the code

This method can’t help filtering out duplicate nans, but it can also filter out objects.

  • Advantages: This method allows for duplicationString,Boolean,Number,undefined,null,NaN,Object, returns the original array after the deduplication.
  • Disadvantages: For NaN and ‘NaN’, the key of the object is treated as a key and cannot distinguish NaN from ‘NaN’.

Method 6 uses the properties of ES6 Set data structure

All elements in a Set are unique

{
    let arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.'NaN'.0.0.'a'.'a', {}, {}];function distinct(arr) {
        return Array.from(new Set(arr))
    }

    console.log(distinct(arr)) / / (1, "true", true, 15, false, undefined, null, NaN, "NaN," 0, "a", {...}, {...}]
}
Copy the code
  • Advantages: This method allows for duplicationString,Boolean,Number,undefined,null,NaN, returns the new array with the rehash removed.
  • Disadvantages: Cannot filter duplicatesObject.

Method 7 uses the characteristics of Map data structure of ES6 to eliminate duplication

{
    let arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.'NaN'.0.0.'a'.'a', {}, {}];function distinct(arr) {
        let map = new Map(a)let newArr = []
        for(let i = 0; i < arr.length; i++) {
            if(! map.has(arr[i])) { map.set(arr[i]) newArr.push(arr[i]) } }return newArr
    }

    console.log(distinct(arr)) / / (1, "true", true, 15, false, undefined, null, NaN, "NaN," 0, "a", {...}, {...}]
}
Copy the code
  • Advantages: This method allows for duplicationString,Boolean,Number,undefined,null,NaN, returns the new array with the rehash removed.
  • Disadvantages: Cannot filter duplicatesObject.

Note: Map stores memory addresses

Method 8 uses sort() to remove weights

{
    let arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.'NaN'.0.0.'a'.'a', {}, {}];function distinct(arr) {
        let sortArr = arr.sort()
        let newArr = []
        for(let i = 1; i < sortArr.length; i++) {
            if(sortArr[i] ! == sortArr[i- 1]) {
                newArr.push(arr[i])
            }
        }
        return newArr
    }

    console.log(distinct(arr)) / / [1, 15, NaN, NaN, "NaN," {...}, {...}, "a", false, null, "true", true, undefined]
}
Copy the code

Defects of the method, obviously, in view of the ‘true’, ‘true, true, true, undefined, undefined, null, null, NaN, NaN, 0, 0, {}, {} are not good filtering to heavy, it is not recommended to use this method to weight.

The reduce method is used to deduplicate an array

{
    let arr = [1.1.'true'.'true'.true.true.15.15.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.'NaN'.0.0.'a'.'a', {}, {}];function distinct(arr) {
        return arr.sort().reduce((init, current) = > {
            if(init.length === 0 || init[init.length- 1] !== current) {
                init.push(current);
            }
            returninit; } []); }console.log(distinct(arr)) / / [0, 1, 15, NaN, NaN, "NaN," {...}, {...}, "a", false, null, "true", true, undefined]
}
Copy the code

This method sorts the array first and then de-filters NaN and Objects that cannot be duplicated.

extension

Set (MDN) [developer.mozilla.org/en-US/docs/…].

Map (MDN) [developer.mozilla.org/zh-CN/docs/…].

A friend of mine recently encountered this problem in an interview. The interviewer asked how to remove NaN and Object. ✌ ️