Medium javascript

Flatten nested array (one level)

Problem

Flatten [[1,2],[3],[4,5]] to [1,2,3,4,5].

Hints
  • Array.prototype.flat()

Your practice code

Ready — edit the code above and click Run.

Solution

const nested = [[1, 2], [3], [4, 5]];
console.log(nested.flat());

Try solving on your own first, then reveal the official answer.

Explanation

flat() with depth 1. Deep flatten uses flat(Infinity) or recursion.

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details