No.1

Reuse deduplication before ES6 update. Main idea: double loop +splice() method

var arr = [1.1.2.2.3.3.5.5.null.null.NaN.NaN, {}, {}]
for (let i = 0; i < arr.length - 1; i++) {
    for (let j = i + 1; j < arr.length; j++) {
        if (arr[i] === arr[j]) {
            arr.splice(j, 1)
            j--  // Note that there is a rollback here}}}console.log(arr);
//[1, 2, 3, 5, null, NaN, NaN, {}, {}]
// NaN and {} cannot be rewritten. Null can be rewritten
Copy the code

No.2

JS in the simplest way to heavy. Set+ extension operator (…)

var arr = [1.1.2.2.3.3.5.5.null.null.NaN.NaN, {}, {}]
console.log([...new Set(arr)]);

// The element in Set is used only
Console. log(array. from(new Set(arr))) is also OK
//[1, 2, 3, 5, null, NaN, {}, {}]
{} can be deleted from null, NaN
Copy the code

No.3

Use JS to determine the presence or absence of elements. 1 layer loop +indexOf/includes

var arr = [1.1.2.2.3.3.5.5.null.null.NaN.NaN, {}, {}]
var array = []
for (let i = 0; i < arr.length; i++) {
    if (array.indexOf(arr[i]) === -1) {
        array.push(arr[i])
    }
}
console.log(array);
//[1, 2, 3, 5, null, NaN, NaN, {}, {}]
// NaN and {} cannot be undone. Null can be undone
Copy the code
var arr = [1.1.2.2.3.3.5.5.null.null.NaN.NaN, {}, {}]
var array = []
for (let i = 0; i < arr.length; i++) {
    if(! array.includes(arr[i])) { array.push(arr[i]) } }console.log(array);
// The result is exactly the same as above
Copy the code

No.4

If you don’t want to write loops… . Try this: filter+indexOf

var arr = [1.1.2.2.3.3.5.5.null.null.NaN.NaN, {}, {}]
var array = []
array = arr.filter((item, index) = > {
    // During each iteration, the index changes constantly, but the position of the first occurrence of the item is fixed
    return arr.indexOf(item) === index  
})
console.log(array);
//[1, 2, 3, 5, null, NaN, {}, {}]
{} can be deleted from null, NaN
Copy the code

No.5

All of the above methods are flawed. Check out the perfect way to lose weight: filter+hasOwnProperty

var arr = [1.1.2.2.3.3.5.5.null.null.NaN.NaN, {}, {}]
var array = []
var obj = {}
array = arr.filter(item= > {
    //console.log(typeof item + item); Typeof item + item = typeof item + item
    if (obj.hasOwnProperty(typeof item + item)) {
    return false
  } else {
    obj[typeof item + item] = true
    return true}})console.log(array);
////[1, 2, 3, 5, null, NaN, {}]
// hasOwnPrototype() does not climb the prototype chain. Check whether there is a data type + data on the obj object itself
// Return false if it exists and filter it out;
// If it does not exist, store this data type + data, and return true, indicating no filtering.
Copy the code

Write last: As you can see, use it on a case-by-case basis (browser compatibility/need to handle special elements)