JS Built-ins
TypeScriptbeginner

Generics β€” flatten<T>

πŸ“– Usage Example
flatten([[1, 2], [3, 4], [5]])   // β†’ [1, 2, 3, 4, 5]

flatten<T> collapses one level of nesting, returning a flat array of the same element type.

Generics: flatten<T>

Challenge: Implement flatten<T>(arr: T[][]): T[] collapsing one level of nesting.

flatten([[1,2],[3,4]])  // β†’ [1,2,3,4]
πŸ’‘ Hints (1)
  • β€’ Array.prototype.flat() with default depth=1 does exactly this.