数组扁平化
1.使用arr.flat()
const arr = [1,[2,[3,[4,5]]], 6]
const res = arr.flat(Infinity)
![](https://img2020.cnblogs.com/blog/2261524/202106/2261524-20210610151254673-1230066677.png)
2.使用正则
const arr = [1,[2,[3,[4,5]]], 6]
const res = JSON.stringify(arr).split(',').map(n=>Number.parseInt(n))
![](https://img2020.cnblogs.com/blog/2261524/202106/2261524-20210610152052695-150666188.png)
3.正则版本2
const arr = [1,[2,[3,[4,5]]], 6]
const res = JSON.parse('[' + JSON.stringify(arr).replace(/\[|\]/g, '') + ']')
![](https://img2020.cnblogs.com/blog/2261524/202106/2261524-20210610152603558-1777210820.png)
数组去重
1.使用Set
const arr = [1, 1, '1', 17, true, true, false, false, 'true', 'a', {}, {}]
const res = Array.from(new Set(arr))
![](https://img2020.cnblogs.com/blog/2261524/202106/2261524-20210610153213695-1613244151.png)
2.使用indexOf
const arr = [1, 1, '1', 17, true, true, false, false, 'true', 'a', {}, {}]
const res = []
for(let i=0; i< arr.length; i++){
if(res.indexOf(arr[i]) === -1) res.push(arr[i])
}
![](https://img2020.cnblogs.com/blog/2261524/202106/2261524-20210610154315101-953785865.png)
2.使用includes
const arr = [1, 1, '1', 17, true, true, false, false, 'true', 'a', {}, {}]
const res = []
for(let i=0; i< arr.length; i++){
if(!res.includes(arr[i])) res.push(arr[i])
}
3.使用filter
const arr = const arr = [1, 1, '1', 17, true, true, false, false, 'true', 'a', {}, {}]
const res = arr.filter((item, index)=> arr.indexOf(item)===index)
![](https://img2020.cnblogs.com/blog/2261524/202106/2261524-20210610155051190-629037760.png)