JS Built-ins
TypeScriptintermediate

Advanced β€” flatMap<T, U>

πŸ“– Usage Example
flatMap([1, 2, 3], x => [x, x * 2])   // β†’ [1, 2, 2, 4, 3, 6]

flatMap<T, U> maps each element to an array then concatenates all results into one flat array.

Advanced: flatMap

Challenge: Implement flatMap<T, U>(arr: T[], fn: (x: T) => U[]): U[] mapping then flattening one level.

flatMap([1, 2, 3], x => [x, x * 2])  // β†’ [1, 2, 2, 4, 3, 6]
πŸ’‘ Hints (1)
  • β€’ Array.prototype.flatMap maps then flattens one level.