How to Flatten an Array in JavaScript
Working with arrays in JavaScript is common — but what happens when an array contains other arrays inside it? That’s called a nested array, and sometimes, you just want to flatten it into one single-level array.
In this post, we’ll explore:
- ✅ What it means to flatten an array
- 🔧 Different methods to do it
- 💻 JavaScript code examples
🧱 What is a Nested Array?
A nested array means an array that contains one or more arrays inside it. For example:
arr = [1, [2, 3], [4, [5, 6]]];
Your goal is to flatten it to:
[1, 2, 3, 4, 5, 6]