Array.prototype.groupBy
Group an array into buckets by a key function — now a real language feature as Object.groupBy.
JavaScript · ES6 — Pratik Rai ·
Yield every value in a nested array one at a time, so an enormous structure is never flattened into memory.
Write a generator function inorderTraversal(arr) that yields every integer in a multidimensional array, in order.
Because it is a generator, the caller can take values one at a time and stop early — nothing is materialised up front.
Input:
JSfile.javascript1[...inorderTraversal([1, [2, 3], [[4]]])];
Output:
[1, 2, 3, 4]
Spreading drains the generator, but next() one value at a time works just as well.
yield* delegates to a recursive call and forwards every value from it.next() does.Goal: Yield each value lazily, delegating into nested arrays.
Continue learning with these related challenges
Group an array into buckets by a key function — now a real language feature as Object.groupBy.
JavaScript · ES6 — Pratik Rai ·
The same result as the recursive flatten, with your own stack instead of the call stack.
JavaScript · ES6 — Pratik Rai ·
Implement a function to flatten a nested array in JavaScript.
JavaScript · Arrays · Recursion — Pratik Rai ·
Group an array into buckets by a key function — now a real language feature as Object.groupBy.
JavaScript · ES6
Pratik Rai ·
The same result as the recursive flatten, with your own stack instead of the call stack.
JavaScript · ES6
Pratik Rai ·
Implement a function to flatten a nested array in JavaScript.
JavaScript · Arrays · Recursion
Pratik Rai ·