Flatten Nested Arrays

A recursive function to flatten a deeply nested array.

TypeScript
david

13 weeks ago

function flattenArray<T>(arr: any[]): T[] {
  return arr.reduce(
    (flat, item) =>
      Array.isArray(item) ? [...flat, ...flattenArray(item)] : [...flat, item],
    [] as T[]
  );
}
0