JS Built-ins
← Array95 / 226
ArrayArray.prototype.flatMapintermediate

Array.prototype.flatMap() β€” expand ranges

πŸ“– Usage Example
const ranges = [2, 3]
ranges.flatMap(n => Array.from({length: n}, (_, i) => i))
// β†’ [0, 1, 0, 1, 2]

Use flatMap() to expand each element into a sequence of values.

Array.prototype.flatMap() β€” generating sequences

flatMap() can expand each element into multiple elements. For example, each [start, end] pair can be expanded into the full range.

Challenge: Implement expandRange(pairs) where each [start, end] pair expands to [start, start+1, ..., end].

expandRange([[1, 3], [6, 8]]) // β†’ [1, 2, 3, 6, 7, 8]
expandRange([[5, 5]])         // β†’ [5]
πŸ’‘ Hints (2)
  • β€’ `Array.from({ length: end - start + 1 }, (_, i) => start + i)` generates the range sequence.
  • β€’ `flatMap` then flattens all these range arrays into one.