Transformation vs Selection vs Aggregation
Use map to transform each element into new form.
Use filter to keep only matching elements.
Use reduce for aggregated output such as totals, grouped objects, or summary metrics.
JavaScript Tutorial
Array methods are essential for transforming and processing data in modern JavaScript applications.
Why this matters
Clean array operations improve readability, reduce loop bugs, and accelerate feature development.
Use map to transform each element into new form.
Use filter to keep only matching elements.
Use reduce for aggregated output such as totals, grouped objects, or summary metrics.
Use find for first match, filter for all matches, and includes for simple primitive presence checks.
sort mutates arrays. Clone with spread before sorting when immutability matters.
const orders = [
{ id: 1, amount: 500, paid: true },
{ id: 2, amount: 1200, paid: false },
{ id: 3, amount: 750, paid: true },
];
const paidTotal = orders
.filter((o) => o.paid)
.map((o) => o.amount)
.reduce((sum, amt) => sum + amt, 0);
console.log(paidTotal);A standard business logic flow in dashboards and reports.
const products = [
{ sku: "A1", stock: 0 },
{ sku: "B2", stock: 12 },
{ sku: "C3", stock: 5 },
];
console.log(products.find((p) => p.stock > 0));
console.log(products.filter((p) => p.stock > 0));find gives first match; filter gives full list.
const nums = [9, 2, 6, 1];
const sorted = [...nums].sort((a, b) => a - b);
console.log(nums); // original intact
console.log(sorted);Clone first to avoid mutating source array.
Use forEach for side effects; reserve map for returned transformed arrays.
Pass explicit initial accumulator for predictable behavior.
Clone before using sort/splice when immutable behavior is required.
Use whichever is clearer. reduce is powerful but should remain readable.
Yes, native sort mutates. Clone first if you need original preserved.
Only while readable. Break into named steps when logic grows complex.
map, filter, reduce, find, sort, and flat/flatMap are frequently discussed.