#Generators#Iterators

Flatten a Nested Array With a Generator

Yield every value in a nested array one at a time, so an enormous structure is never flattened into memory.

By Pratik RaiMedium

Write a generator function flattenNested(arr) that walks an array nested to any depth and yields its numbers one by one, left to right.

Because it is a generator, the caller can take values one at a time and stop early — nothing is materialised up front.

Examples

Input:

JSfile.javascript
1[...flattenNested([1, [2, 3], [[4]]])];

Output:

[1, 2, 3, 4]

Spreading drains the generator, but next() one value at a time works just as well.

Constraints

  • Every item is either a number or another array
  • Any array, at any level, may be empty

Notes

  • yield* delegates to a recursive call and forwards every value from it.
  • Calling a generator function runs none of its body — the first next() does.

How to solve it

Yielding instead of returning

A function that returns an array has to build the whole array first. A generator hands back one value at a time, so a caller who wanted the first three can stop after three.

Approach

yield* delegates to a recursive call and forwards every value it produces.

Implementation

JSfile.javascript
1function* flattenNested(arr) { 2 for (const item of arr) { 3 if (Array.isArray(item)) yield* flattenNested(item); 4 else yield item; 5 } 6}

Worth knowing

Calling a generator function runs none of its body — it returns an iterator, and the first next() starts execution. That laziness is the entire reason to reach for one.

yield* is doing real work here: without it you would loop over the inner generator and yield each value manually. With it, the delegation is one line and the recursion reads like the non-generator version.

Goal: Yield each value lazily, delegating into nested arrays.

Frequently asked questions

What is a generator function in JavaScript?
A function declared with function* that can pause at each yield and resume where it left off. Calling it returns an iterator rather than running the body.
Why use a generator instead of returning an array?
Because values are produced on demand. A caller that only needs the first few never pays for the rest, and an enormous or infinite structure never has to exist in memory at once.
What does yield* do?
It delegates to another iterable and forwards every value it produces. For a recursive traversal it replaces a manual loop over the inner generator with a single line.
When does the body of a generator actually run?
Not when you call it — that only creates the iterator. The body starts on the first next() and pauses again at each yield, which is exactly what makes it lazy.

Related Challenges

Continue learning with these related challenges

View All
JavaScript

Array.prototype.groupBy

Group an array into buckets by a key function — now a real language feature as Object.groupBy.

JavaScript · ES6 — Pratik Rai ·

JavaScript

Flatten an Array Without Recursion

The same result as the recursive flatten, with your own stack instead of the call stack.

JavaScript · ES6 — Pratik Rai ·

JavaScript

Flatten Nested Array in JavaScript

Implement a function to flatten a nested array in JavaScript.

JavaScript · Arrays · Recursion — Pratik Rai ·