First, use filter to remove weight
let arr1 = [1.1.'true'.'true'.true.true.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.0.0.'a'.'a', {}, {}, [], []1], [2]].let arr2 = arr1.filter((el, index, arr) = > index == arr.indexOf(el));
console.log(arr2)
//[1,"true",true,false,undefined,null,"NaN",0,"a",{},{},[],[],[1],[2]]
Copy the code

There are two problems with this method: (1) you can see that NaN is missing in the output above, and (2) you can’t determine the reference type

Second, use Set to remove weight

There are two ways to write a Set to undo:

The first:

let arr1 = [1.1.'true'.'true'.true.true.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.0.0.'a'.'a', {}, {}, [], []1], [2]].let arr2 = Array.from(new Set(arr1))
console.log(arr2)
//[1,"true",true,false,undefined,null,NaN,"NaN",0,"a",{},{},[],[],[1],[2]]
Copy the code

The second:

let arr1 = [1.1.'true'.'true'.true.true.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.0.0.'a'.'a', {}, {}, [], []1], [2]].let arr2 = [...new Set(array)]
console.log(arr2)
//[1,"true",true,false,undefined,null,NaN,"NaN",0,"a",{},{},[],[],[1],[2]]
Copy the code

Both methods yield similar results, and the second method is also used to remove duplicate characters from the string.

3. Use reduce function to remove weight
let arr1 = [1.1.'true'.'true'.true.true.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.0.0.'a'.'a', {}, {}, [], []1], [2]].let arr2 = arr1.reduce((arr, el) = > arr.includes(el) ? arr : [...arr, el], []);
​
console.log(arr2)
//[1,"true",true,false,undefined,null,NaN,"NaN",0,"a",{},{},[],[],[1],[2]]
Copy the code
4. Use MAP to remove weight
let arr1 = [1.1.'true'.'true'.true.true.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.0.0.'a'.'a', {}, {}, [], []1], [2]].let map1 = new Map(a); arr1.forEach((el, i) = > map1.set(el, i))
let arr2 = []
map1.forEach((el, i, self) = > arr2.push(i))
​
console.log(arr2)
//[1,"true",true,false,undefined,null,NaN,"NaN",0,"a",{},{},[],[],[1],[2]]
Copy the code

5. Use the for loop to remove weight

let arr1 = [1.1.'true'.'true'.true.true.false.false.undefined.undefined.null.null.NaN.NaN.'NaN'.0.0.'a'.'a', {}, {}, [], []1], [2]].let arr2 = [];
for (let i = 0, len = arr1.length; i < len; i++) {
    if (arr2.indexOf(arr1[i]) == -1) {
        arr2.push(arr1[i])
    }
}
console.log(arr2)
//[1,"true",true,false,undefined,null,NaN,"NaN",0,"a",{},{},[],[],[1],[2]]
Copy the code