With a large array flatten() fails with a stack overflow error.
A (much bigger) implementation that works:
function flatten(array: any[]) {
const result: any[] = [];
for (const element of array) {
if (Array.isArray(element)) {
for (const inner of element) {
result.push(inner);
}
} else {
result.push(element);
}
}
return result;
}
With a large array
flatten()
fails with a stack overflow error.A (much bigger) implementation that works: