Skip to Content
type Flatten<T> = T extends [infer F, ...infer R] ? F extends any[] ? Flatten<[...F, ...R]> : [F, ...Flatten<[...R]>] : T;
const arr = [1, 2, [3, 4, [5, 6, [7]], [8], [9], 10], 11]; function flat(arr) { const list = []; arr.forEach((i) => { if (Array.isArray(i)) { list.push(flat(i).slice()); } else { list.push(i); } }); return list; } console.log(flat(arr)); console.log(arr.flat(Infinity)); // [ // 1, 2, 3, 4, 5, // 6, 7, 8, 9, 10, // 11 // ]
Last updated on