Merge/flatten an array of arrays
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Flattening an array of arrays means removing one or more nesting levels so that the inner elements become part of a single outer array. In JavaScript, the best technique depends on how deeply nested the data is and whether you only need a one-level merge or a fully recursive flatten.
One-Level Flattening with flat()
If your structure is a simple array of arrays, Array.prototype.flat() is the clearest built-in option.
Output:
This removes one nesting level only, which is exactly what most “merge an array of arrays” problems mean.
Deeper Nesting
If the data is nested more deeply, pass an explicit depth:
If you truly want to flatten every nested array regardless of depth:
That is powerful, but it also means you are intentionally destroying all nested grouping. Make sure that matches the real data model.
reduce() for Controlled Flattening
flat() is concise, but reduce() is useful when you want more control or need to support older environments.
This is especially handy when you also want to transform or filter elements as you flatten them.
Older Pattern: concat with Spread
Another one-level technique is:
This works because the inner arrays are spread as separate arguments to concat. It still appears in older codebases that predate widespread use of flat().
Recursive Flattening
If you need full control over what counts as flattenable, write a recursive helper.
This gives you a place to add custom rules, such as preserving certain nested structures or rejecting non-array containers.
Merge vs Flatten
People often use “merge” and “flatten” interchangeably, but there is a subtle difference:
- merge usually means joining sibling arrays into one
- flatten can mean removing arbitrary nesting depth
For [[1, 2], [3, 4]], both words describe the same outcome. For [1, [2, [3]]], “flatten” is the more precise term.
Being explicit about the required depth helps avoid bugs and over-flattening.
Performance Notes
For everyday code, choose readability first. All of these methods are fine for normal application sizes. If the arrays are extremely large, performance differences can matter, but you should profile before optimizing based on style alone.
In practical code, the larger source of bugs is not raw speed. It is flattening the wrong amount of structure.
Common Pitfalls
- Assuming
flat()removes all nesting by default when it only removes one level unless given a depth. - Flattening arrays whose inner grouping is semantically meaningful, such as pages, transactions, or teams.
- Using recursive flattening blindly on data that may contain very deep or irregular nesting without considering stack depth and data shape.
- Choosing an older
concatorreducepattern whenflat()would communicate intent more clearly. - Forgetting that flattening changes the data model, not just the syntax of access.
Summary
- Use
flat()for the clearest built-in way to flatten arrays in modern JavaScript. - Pass a depth such as
2orInfinitywhen the nesting goes deeper than one level. - Use
reduce()or recursion when you need custom flattening behavior. - '
[].concat(...arrays)is a valid older one-level merge pattern.' - Decide whether you want a one-level merge or a full flatten before picking the implementation.

