← Home ← Codex ← DEBT ← Engine
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 141
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 1 ping W 0 pings T 1 ping F 1 ping S 0 pings S 1 ping M 1 ping T 1 ping W 0 pings T 1 ping F 0 pings S 1 ping S 1 ping M 0 pings T 1 ping W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 15 Google 12 Ahrefs 7 Bing 7 PetalBot 7 ChatGPT 6 Perplexity 6 Unknown AI 5 SEMrush 5 Scrapy 4 Twitter/X 4 Claude 3 Brave Search 2 Sogou 2 Applebot 2 Majestic 1 Meta AI 1
crawler 83 crawler_json 3 pre-tracking 3
🧱 FUNDAMENTALS — new to this? Start with the ground floor.
JavaScript javascript The programming language of the browser — it reads and modifies the page (the DOM), reacts to user events, and fetches data without reloading.

JavaScript is the only language browsers execute, so every interactive behaviour on the web goes through it. Its two defining traits — single-threaded event loop and loose typing (== coercion) — explain the majority of both its bugs and its design patterns.

💡 Default to const, use === always, and reach for let only when a value genuinely reassigns.

Ask Codex about JavaScript →
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