前端面试之常考题:
将一个数组扁平化并去重,最终得到一个升序且不重复的数组。
总结常用的三种方法:
const arr = [[1,2,3,],[3,4,5,5],4,6,7,[11,13,[12,14]],20];
const arr2 = [...new Set(arr.flat(Infinity).sort((x,y)=>x-y))];
console.log(arr2) //[ 1, 2, 3, 4, 5, 6, 7, 11, 12, 13, 14, 20 ]
const arr = [[1,2,3,],[3,4,5,5],4,6,7,[11,13,[12,14]],20];
const arr2 = Array.from(new Set(arr.flat(Infinity).sort((x,y)=>x-y)));
console.log(arr2) //[ 1, 2, 3, 4, 5, 6, 7, 11, 12, 13, 14, 20 ]
const arr = [[1,2,3,],[3,4,5,5],4,6,7,[11,13,[12,14]],20];
const arr2 = arr.toString().split(",").sort((a,b)=>{ return a-b}).map(Number)
console.log(arr2) //[ 1, 2, 3, 4, 5, 6, 7, 11, 12, 13, 14, 20 ]
我的博客记载前端所需知识点和面试题,欢迎来撩~个人博客