Please check this utility function I wrote earlier.
// NestedArray<T> represents T or Array of T or Array of Array of T .....
// let nestedNumbers: NestedArray<number> = [[[[[1]]]]];
export type NestedArray<T> = Array<T> | Array<NestedArray<T>>;
// Able to flatten deeply nested array
// flattenArray(nestedNumbers) should produce => [1] : Array<number>
export const flattenArray = <T>(arr: NestedArray<T>): Array<T> => {
if (!Array.isArray(arr)) return arr ? [arr] : [];
return arr.reduce<Array<T>>((acc: Array<T>, item: NestedArray<T>) => {
if (Array.isArray(item)) {
return [...acc, ...flattenArray(item)];
}
return [...acc, item];
}, []);
}