Array to heavy whether it is an interview or ordinary business will often encounter, general students will see the mouth slightly up, backhand is a nested cycle, hehe ~ deep work and name….

Is there any other way to get rid of it? Here are some fancy ways to lose weight!!

1, Set

How it works: A Set is like an array, but the values of its members are unique and it takes an array or an array of classes as an argument

let arr1 = [1.1.2."true".true.true.undefined.undefined.null.null.NaN.NaN, {}, {}, {a:1}, {a:1}]
let result1 = Array.from(new Set(arr1))
console.log(result1)  // (11) [1, 2, "true", true, undefined, null, NaN, {}, {}, {a:1}, {a:1}]
Copy the code

IndexOf () + loop

Principle: Create an empty array to determine whether it exists, and push it into a new array if it does not

let arr2 = [1.1.2."true".true.true.undefined.undefined.null.null.NaN.NaN, {}, {}, {a:1}, {a:1}]
let result2 = [];
for (let i = 0; i < arr2.length; i++) {
	if (result2.indexOf(arr2[i]) === -1) { // indexOf() === -1 indicates no search
		result2.push(arr2[i])
	}
}
console.log(result2) // (12) [1, 2, "true", true, undefined, null, NaN, NaN, {}, {}, {a:1}, {a:1}]
Copy the code

3, splice() + nested loop

The splice() method deletes and alters the array, then deletes through a double loop

let arr3 = [1.1.2."true".true.true.undefined.undefined.null.null.NaN.NaN, {}, {}, {a:1}, {a:1}]
for (let i = 0; i < arr3.length; i++) {
	for (let j = i + 1; j < arr3.length; j++) {
		if (arr3[i] === arr3[j]) {
			arr3.splice(j, 1)  //splice() The first parameter indicates which element to start from, and the second parameter indicates how many elements to delete}}}console.log(arr3) // (12) [1, 2, "true", true, undefined, null, NaN, NaN, {}, {}, {a:1}, {a:1}]
Copy the code

4, sort()+ before and after element comparison

We sort the array using sort() so that the same elements are together, and then push the array back and forth if they are different

let arr4 = [1.1.2."true".true.true.undefined.undefined.null.null.NaN.NaN, {}, {}, {a:1}, {a:1}]
arr4 = arr4.sort();
let result4 = [arr4[0]]
for (let i = 1; i < arr4.length; i++) {
    if(arr4[i] ! == arr4[i-1]) { // Compare the last one with the previous one
		result4.push(arr4[i])
	}
}
console.log(result4) // (12) [1, 2, NaN, NaN, {}, {}, {a:1}, {a:1}, null, "true", true, undefined]
Copy the code

5. Object attribute judgment

Principle: The use of object attributes can not be the same to duplicate, we can construct an empty object, then the array element into the object attributes, determine whether the attributes exist, if not, push into a new array. The disadvantage is that object attributes are implicitly converted to strings, such as true and ‘true’ are judged to be the same

let arr5 = [1.1.2."true".true.true.undefined.undefined.null.null.NaN.NaN, {}, {}, {a:1}, {a:1}]
let result5 = [], obj5 = {};
for (let i = 0; i < arr5.length; i++) {
	if(! obj5[arr5[i]]) { result5.push(arr5[i]) obj5[arr5[i]] =1; }}console.log(result5) // (7) [1, 2, "true", undefined, null, NaN, {}]
Copy the code

6, includes() + loop

How it works: Similar to indexOf() except that it returns a Boolean and if not, pushes it into a new array

let arr6 = [1.1.2."true".true.true.undefined.undefined.null.null.NaN.NaN, {}, {}, {a:1}, {a:1}]
let result6 = [];
for (let i = 0; i < arr6.length; i++) {
	if(! result6.includes(arr6[i])) {// If it does not exist
		result6.push(arr6[i])
	}
}
console.log(result6) // (11) [1, 2, "true", true, undefined, null, NaN, {}, {}, {a:1}, {a:1}]
Copy the code

7, filter + indexOf()

Principle: Use indexOf() to determine whether the index positions of elements are the same. If they are not the same, it indicates that they already exist, and then filter them out

let arr7 = [1.1.2."true".true.true.undefined.undefined.null.null.NaN.NaN, {}, {}, {a:1}, {a:1}]
let result7 = arr7.filter((item, index, arr7) = > {
	return arr7.indexOf(item) === index // Determine each value index position
})
console.log(result7) // (10) [1, 2, "true", true, undefined, null, {}, {}, {a:1}, {a:1}]
Copy the code

8. Map set of key-value pairs

Principle: Similar to the object of the key-value pair set, the use of the key can not repeat the characteristics, the advantage is that the key can be how the data type, can be said to be the optimization of the object

let arr8 = [1.1.2."true".true.true.undefined.undefined.null.null.NaN.NaN, {}, {}, {a:1}, {a:1}]
let result8 = [], map = new Map(a);for (let i = 0; i < arr8.length; i++) {
	if(! map.has(arr8[i])) { result8.push(arr8[i]) map.set(arr8[i],1)}}console.log(result8) // (11) [1, 2, "true", true, undefined, null, NaN, {}, {}, {a:1}, {a:1}]
Copy the code

Conclusion:

In fact, the above method basically uses the loop, but the method of determining the element is slightly different. Note that basically all method comparisons are determined by ===, but NaN is a column, and NaN===NaN also returns true, so it is best to remove NaN from the data. There are also ways to compare nans, such as Set, Map, includes(), and so on


This article is a summary of the author, if biased, welcome to leave a message correction, if you think this article is useful to you, might as well point a praise ~ about the author: GitHub Jane book nuggets