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

Functional Array Methods (JS)

javascript ES2015 Beginner
debt(d3/e1/b3/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 with automated: yes, and the code_pattern describes for-loops building result arrays and forEach-with-side-effects — both patterns ESLint rules (e.g. prefer-array-methods or no-array-for-each) can flag out of the box or with common plugins.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix is literally a direct substitution — replace forEach with map, filter()[0] with find(), map+flat with flatMap. Each fix is a single call replacement at the usage site.

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

Closest to 'localised tax' (b3). The misuse (e.g. forEach instead of map) is scoped to individual call sites. It doesn't propagate architecture-wide — each instance is independently fixable and doesn't shape the broader codebase. The applies_to covers web and cli broadly, but the impact of each mistake is local.

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

Closest to 'notable trap (a documented gotcha most devs eventually learn)' (t5). The misconception field explicitly states that forEach and map appear interchangeable since both iterate, but forEach returns undefined and discards return values while map collects them. This is a well-known documented gotcha that many developers encounter and learn, but it's not catastrophic or system-wide — scoring t5 exactly.

About DEBT scoring →

Also Known As

JS array methods map filter reduce JS Array prototype methods

TL;DR

map, filter, reduce, find, flatMap, every, some — JavaScript's built-in higher-order array methods for expressive data transformation.

Explanation

JavaScript arrays have rich functional methods. map(fn) transforms every element, returning a new array. filter(fn) keeps elements where fn returns truthy. reduce(fn, init) folds to a single value — the most powerful but most misused. find(fn) returns the first matching element or undefined. findIndex(fn) returns its index or -1. some(fn) returns true if any element matches; every(fn) if all match. flat(depth) flattens nested arrays; flatMap(fn) maps then flattens one level (more efficient than map().flat()). Array.from(iterable, mapFn) converts iterables to arrays with optional mapping. sort() mutates the original — use [...arr].sort() to sort a copy. All these methods compose cleanly: users.filter(u => u.active).map(u => u.email).join(', ').

Diagram

flowchart LR
    ARR[array] -->|map| TRANSFORM[new array<br/>same length<br/>values transformed]
    ARR -->|filter| SUBSET[new array<br/>shorter<br/>matching items only]
    ARR -->|reduce| ACCUM[single value<br/>sum object string etc]
    ARR -->|find| FIRST[first matching item<br/>or undefined]
    ARR -->|some| BOOL1[true if any match]
    ARR -->|every| BOOL2[true if all match]
    ARR -->|flatMap| FLAT[map then flatten<br/>one level]
    INFO[All non-mutating<br/>original array unchanged]
style ARR fill:#6e40c9,color:#fff
style TRANSFORM fill:#238636,color:#fff
style SUBSET fill:#1f6feb,color:#fff
style ACCUM fill:#d29922,color:#fff
style INFO fill:#238636,color:#fff

Common Misconception

forEach and map are interchangeable since both iterate over an array. forEach executes for side effects and returns undefined. map returns a new array of transformed values. Using map and discarding the result wastes an allocation; using forEach when you need a transformed array requires an external variable.

Why It Matters

JavaScript's array methods (map, filter, reduce, find, some, every) express transformations declaratively — replacing manual loops with them makes code more readable and less error-prone.

Common Mistakes

  • Using forEach when map is needed — forEach discards return values; map collects them.
  • Using filter().map() when flatMap() would be cleaner for mapping and flattening.
  • Not using find() when you want the first matching element — filter()[0] returns undefined awkwardly.
  • Mutating the original array in map() callbacks — map should return a new transformed value.

Code Examples

✗ Vulnerable
// Manual loop instead of array method:
const names = [];
for (let i = 0; i < users.length; i++) {
    names.push(users[i].name); // Use: users.map(u => u.name)
}

// forEach misused for transformation:
const doubled = [];
numbers.forEach(n => doubled.push(n * 2)); // Use: numbers.map(n => n * 2)
✓ Fixed
const totals = orders
  .filter(o => o.status === 'paid')
  .map(o => o.amount)
  .reduce((sum, amt) => sum + amt, 0);

Added 15 Mar 2026
Edited 22 Mar 2026
Views 29
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 2 pings T 1 ping F 2 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 2 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 1 ping F
No pings yesterday
Amazonbot 9 Perplexity 4 Ahrefs 3 Google 3 Unknown AI 3 ChatGPT 2 Majestic 1 Bing 1
crawler 22 crawler_json 2 pre-tracking 2
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Use map/filter/reduce for data transformations instead of imperative loops; prefer flatMap over map+flat; use Array.from() to convert iterables
📦 Applies To
javascript ES2015 web cli
🔗 Prerequisites
🔍 Detection Hints
for loop building result array that map/filter/reduce would express more clearly; forEach with side effects instead of map
Auto-detectable: ✓ Yes eslint
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Low Context: Function

✓ schema.org compliant