Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Flatten Nested Array

A Closer Look at Functionsmedium

Course · Section 10: A Closer Look at Functions · Lecture 137: First-Class and Higher-Order Functions

Implement `flattenDeep(arr)` that recursively flattens a deeply nested array into a single flat array.

Sample tests

Input: flattenDeep([1,[2,[3,[4]]]])
Output: [1,2,3,4]
Input: flattenDeep([1,2,3])
Output: [1,2,3]

+ 2 hidden tests run on Submit.

Hints

Common pitfalls
  • Only flattening one level instead of recursing fully.

Learning resources

  • MDN: Array.prototype.flat
Approach & explanation (try first)

Recursing into nested arrays and gathering non-array values flattens arbitrarily deep structures.

Loading...
⌘/Ctrl + Enter

Run your code to see results.