ES6

Set method

The first uses properties unique to a set to de-duplicate it and then converts it to an Array using the array. from method

  <script>
        let arr = [11.22.33.11.22.45.69.45.78.33]
        let newArr = new Set(arr)
        let result = Array.from(newArr)
        console.log(result)
    </script>
Copy the code

The second uses the unique properties of a set to undo it and then uses […] Method to convert it to an array

  <script>
        let arr = [11.22.33.11.22.45.69.45.78.33]
        let newArr = new Set(arr)
        let arr1 = [...newArr]
        console.log(arr1)
    </script>
Copy the code

Before the ES6

Method of element contrast

Every new element that comes in for the first time will return undefined, the else element will be assigned a value of 1, the second element will be deleted, and the else element will be pushed into the new array

  <script>
        let arr = [11.22.33.11.22.45.69.45.78.33]
        let content = [];
        let newArr = [];
        for (var i = 0; i < arr.length; i++) {
            if (content[arr[i]]) {
                delete content[arr[i]];
            } else {
                content[arr[i]] = 1; newArr.push(arr[i]); }}console.log(newArr);
    </script>
Copy the code

Define a new array, place the first element of the original array in the new array, then compare the original array with the new array, if different, push the new array.

 <script>
        let arr = [11.22.33.11.22.45.69.45.78.33]
        let newArr = [arr[0]].for (let i = 1; i < arr.length; i++) {
            let flag = false;
            for (let j = 0; j < newArr.length; j++) {
                if (arr[i] === newArr[j]) {
                    flag = true;
                    break; }}if (!flag) {
                newArr.push(arr[i]);
            }
        }
        console.log(newArr);
    </script>
Copy the code

Sort the original array, compare it with the adjacent array, and push it into the new array if it is different.

 <script>
        let arr = [11.22.33.11.22.45.69.45.78.33]
        let content = arr.sort()
        let newArr = [content[0]]
        for (let i = 1; i < content.length; i++) {
            if(content[i] ! == content[i -1]) {
                newArr.push(content[i])
            }
        }
        console.log(newArr);
    </script>
Copy the code

Using the Array API

IndexOf and lastIndexOf check whether the index is the same by looking at the subscript. There is no essential difference between the two indexes.

 <script>
        let arr = [11.22.33.11.22.45.69.45.78.33]
        let newArr = [];
        for (var i = 0; i < arr.length; i++) {
            if (newArr.indexOf(arr[i]) === -1) {
                newArr.push(arr[i])
            }
        }
        console.log(newArr);;
    </script>
Copy the code
 <script>
        let arr = [11.22.33.11.22.45.69.45.78.33]
        let newArr = [];
        for (var i = 0; i < arr.length; i++) {
            if (newArr.lastIndexOf(arr[i]) === -1) {
                newArr.push(arr[i])
            }
        }
        console.log(newArr);;
    </script>
Copy the code

Includes itself is used to determine whether an array contains an element and returns a Boolean value, but defining a new array and then checking with the original array can achieve the goal of array de-duplication.

<script>
        let arr = [11.22.33.11.22.45.69.45.78.33]
        let newArr = []
        for (var i = 0; i < arr.length; i++) {
            if(! newArr.includes(arr[i])) { newArr.push(arr[i]) } }console.log(newArr);
    </script>
Copy the code

Splice writes two arguments: the first argument is the starting position and the second argument is the number of deletions. If the comparison results are the same, the element will be deleted.

 <script>
        let arr = [11.22.33.11.22.45.69.45.78.33]
        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); }}}console.log(arr);
    </script>
Copy the code

Make use of methods unique to the object

  <script>
        let arr = [11.22.33.11.22.45.69.45.78.33]
        let obj = {}
        let newArr = []
        for (let i = 0; i < arr.length; i++) {
            if(! obj[arr[i]]) { obj[arr[i]] =1
                newArr.push(arr[i])
            }
        }
        console.log(newArr);
    </script>
Copy the code

Methods after all

Actually the above method are many, but most have a problem, that is only for digital to heavy, but there are a lot of array contains not only the digital, there could be a string, object, and so on many elements, so the above method is not feasible at this time, this time you use objects and arrays API, Get rid of some weird arrays.

var newArr = [true, true, false, false, undefined, undefined, null, null, obj = { a: 1}, obj = { a: 1}, ‘abcd’, ‘abcd’, ‘1’, ‘1’, ‘1’, 1, 1], assuming that there is an array of true, False, undefined, null, strings, objects

 <script>
        var newArr = [true.true.false.false.undefined.undefined.null.null, obj = {
            a: 1
        }, obj = {
            a: 1
        }, 'abcd'.'abcd'.'1'.'1'.1.1]

        function unique(arr) {
            var obj = {}
            return arr.filter(function(item, index, array) {
                return obj.hasOwnProperty(typeof item + item) ? false : (obj[typeof item + item] = true)})}console.log(unique(newArr));
    </script>
Copy the code

The output is

And you can see that this array is very special, but it’s been de-duplicated. The main method used here is the hasOwnProperty method of fitler method. The filter method is probably familiar, but hasOwnProperty(). The hasOwnProperty() method returns a Boolean value indicating whether the object has the specified property in its own properties (that is, whether it has the specified key).

Using the hasOwnProperty () method, the prototype + value is used to compare not only the data type, but also the value.