Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Flatten One Level

Working With Arraysmedium

Course · Section 11: Working With Arrays · Lecture 149: Simple Array Methods

Implement `flatten(arr)` flattening one level of nesting.

Sample tests

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

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • flat() also works but flattens deeper if given a depth; one level is the goal here.

Learning resources

  • MDN: Array
Approach & explanation (try first)

Spreading the array into [].concat merges nested arrays exactly one level deep.

Loading...
⌘/Ctrl + Enter

Run your code to see results.