Introduction to the
We see a lot of interview questions about arrays on the Internet. For example, take the following array, flatten it, unduplicate it, and raise it:
let arr = [8[5.9.4].1.3[7.5.10[3.4.6.2]], 4.3.2.4];
Copy the code
In fact, there are many ways to solve this problem, such as using array.prototype. flat, or implementing a flatten function by yourself. We mainly focus on the realization of flat method here.
The first solution
Use the latest grammar
let arr = [8[5.9.4].1.3[7.5.10[3.4.6.2]], 4.3.2.4];
let newArr = Array.from(new Set(arr.flat(Infinity))).sort((a, b) = > {
return a - b;
});
console.log(newArr);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Copy the code
Flat (ES6 syntax); flat(ES6 syntax); flat(ES6 syntax); flat(ES6 syntax); flat(ES6 syntax)
If you don’t know the function of flat, you can refer to MDN array.prototype.flat (), or see Teacher Ruan Yifeng’s introduction of flat. The explanation of Set can refer to MDN Set, or see Teacher Ruan Yifeng’s Set introduction. We will focus here on the implementation of Flat.
Second solution
let arr = [8[5.9.4].1.3[7.5.10[3.4.6.2]], 4.3.2.4];
let newArr = Array.from(new Set(arr.toString().split(",")))
.map(item= > {
return parseInt(item);
})
.sort((a, b) = > {
return a - b;
});
console.log(newArr);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Copy the code
1. We call the toString method on Array to convert it to a string
arr.toString();
8,5,9,4,1,3,7,5,10,3,4,6,2,4,3,2,4 "/ /"
Copy the code
This column will not discuss why the result does not contain the ‘[]’ string. I will write a blog post on the valueOf and toString methods of arrays. Array.prototype.split = array.prototype.split = array.prototype.split = array.prototype.split = array.prototype.split = array.prototype.split = array.prototype.split = array.prototype.split = array.prototype.split = array.prototype.split = array.prototype.split = array.prototype.split = array.prototype.split = array.prototype.split
Third solution
Implement a flatten function without using the array.prototype. flat method by encapsulating a flatten
ES6 implementation
let arr = [8[5.9.4].1.3[7.5.10[3.4.6.2]], 4.3.2.4];
const flatten = arr= >
Array.isArray(arr) ? arr.reduce((a, b) = > [...a, ...flatten(b)], []) : [arr];
let newArr = Array.from(new Set(flatten(arr))).sort((a, b) = > {
return a - b;
});
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Copy the code
ES5
let arr = [8[5.9.4].1.3[7.5.10[3.4.6.2]], 4.3.2.4];
function flatten(arr) {
return Array.isArray(arr)
? arr.reduce(function(prev, current) {
return [...prev, ...flatten(current)];
}, [])
: [arr];
}
let newArr = Array.from(new Set(flatten(arr))).sort((a, b) = > {
return a - b;
});
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Copy the code
Implementation approach
If it is an array, call reduce to implement the merge function. 3. If there is a nested array, recursively call this method
conclusion
There are roughly three ways to implement a flat array
Array.prototype.flat
methodsArray.prototype.toString
Method to string, andsplit
- Implement one yourself
flatten
function