var array = [[1,2,3],4,5,6,[[7]],[]]
var result = flatten(array) //[1, 2, 3, 4, 5, 6, 7]
function flatten(arr){
return arr.reduce((a,b)=>{
return a.concat(Array.isArray(b)?flatten(b):b)
},[])
}
var arr = [[1,3,2,2,3],[2,4],[2,3,4,5,6,[10]],10,8]
function flat(newArr){
return newArr.reduce((arr,item)=>{
return [...new Set(arr.concat(Array.isArray(item)?flat(item):item))].sort((a,b)=>a-b)
},[])
}
console.log(flat(arr)) //[1, 2, 3, 4, 5, 6, 8, 10]
网友评论