← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Array.flat() & Array.flatMap()

JavaScript ES2019 Beginner
debt(d3/e1/b1/t5)
d3 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'default linter catches the common case' (d3). The detection_hints list ESLint as the tool, and the code_pattern 'reduce.*concat|[].concat' is a straightforward pattern ESLint rules can flag, making the misuse detectable with standard linting setup.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix explicitly describes simple one-to-one replacements: reduce-concat patterns become flatMap(), [].concat(...arr) becomes arr.flat(). Each is a direct single-call substitution.

b1 Burden Structural debt — long-term weight of choosing wrong

Closest to 'minimal commitment' (b1). This is a utility array method choice localised to individual call sites. It applies to web and cli contexts but imposes no architectural weight; switching or not using it has no gravitational pull on the rest of the codebase.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception field directly states the canonical wrong belief: developers expect flatMap() to flatten all levels, but it only flattens one level. This is a documented gotcha that many developers encounter, aligning with the t5 anchor of a well-known but non-obvious surprise.

About DEBT scoring →

TL;DR

Array.flat(depth) flattens nested arrays; Array.flatMap() maps then flattens one level — more efficient than map().flat() and great for expanding items.

Explanation

arr.flat() flattens one level by default. arr.flat(Infinity) flattens completely. arr.flat(2) flattens 2 levels. arr.flatMap(fn) is equivalent to arr.map(fn).flat(1) but more efficient. flatMap use cases: expanding one item into multiple (expanding tags into all tag instances), filtering and transforming (return [] to skip, return [item] or [a, b] to keep/expand). flat() replaces manual reduce-concat patterns. Both are ES2019 — use polyfills for IE11 (or Babel).

Watch Out

flat() and flatMap() skip empty slots in sparse arrays and don't preserve them in the result, so [1, , 3].flat() returns [1, 3] — use Array.from() or explicit iteration if you need to preserve holes.

Common Misconception

flatMap() flattens all levels — it only flattens one level. Use flat(Infinity) for deep flattening.

Why It Matters

flatMap() replaces the common but verbose reduce((acc, x) => [...acc, ...fn(x)], []) pattern with a clean, readable alternative.

Common Mistakes

  • Using flatMap() expecting deep flattening — only one level.
  • Not knowing flat() without arguments flattens only 1 level.
  • Using reduce + concat instead of flat()/flatMap() — verbose and slow.

Code Examples

✗ Vulnerable
// Verbose reduce-concat:
const tags = posts.reduce((acc, post) => acc.concat(post.tags), []);

// Manual flat:
const nested = [[1,2],[3,4]];
const flat = [].concat(...nested);
✓ Fixed
const tags = posts.flatMap(post => post.tags);

// Filter + transform in one pass:
const active = items.flatMap(item =>
    item.active ? [{ ...item, label: item.name.toUpperCase() }] : []
);

// Deep flatten:
const deep = nested.flat(Infinity);

Added 23 Mar 2026
Edited 15 Jun 2026
Views 54
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 2 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 2 pings T 0 pings W 1 ping T 1 ping F 1 ping S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 2 pings S 1 ping M 1 ping T 0 pings W
No pings yet today
Google 1
Amazonbot 10 Perplexity 6 Google 5 ChatGPT 4 Unknown AI 4 Scrapy 4 Ahrefs 3 SEMrush 3 Claude 2 Majestic 1 Bing 1 Meta AI 1
crawler 38 crawler_json 3 pre-tracking 3
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Low
⚡ Quick Fix
Replace reduce-concat patterns with flatMap(). Replace [].concat(...arr) with arr.flat(). Use flatMap(x => condition ? [x] : []) for filter+transform.
📦 Applies To
javascript ES2019 web cli
🔗 Prerequisites
🔍 Detection Hints
reduce.*concat|\[\]\.concat
Auto-detectable: ✓ Yes eslint
🤖 AI Agent
Confidence: Medium False Positives: Medium ✓ Auto-fixable Fix: Low Context: Line


✓ schema.org compliant