function flat(arr, depth=1){ return depth>0 && Array.isArray(arr) ? arr.reduce((prev,curr)=>{ if(Array.isArray(curr))return [...prev, ...flat(curr, depth-1)] return [...prev,curr] },[]) :arr } const res = flat([1,2,[3,4,[5,6,[7,8],11]],9,10],3); console.log(res) function flatStack(arr, depth=1){ for(let i = 0;l<arr.length;i++){ let depthNum = depth while(depthNum >0){ if(Array.isArray(arr[i]) { arr.splice(i,1,...arr[i]) depthNum -- } } } }