JavaScript Lesson 5: Arrays — map, filter, reduce

📎 This article includes 2 downloadable practice files ↓

⏱ 1 min read

📘 JavaScript Beginner Course · Lesson 5 of 8

Arrays of objects are JavaScript’s tables. Three methods do most of the work:

“`javascript
const big = orders.filter(o => o.amount > 10000); // like FILTER
const ids = big.map(o => o.id); // transform each row
const total = orders.reduce((s, o) => s + o.amount, 0); // like SUM
const byRegion = orders.reduce((acc, o) => ({ …acc, [o.region]: (acc[o.region] || 0) + o.amount }), {}); // like SUMIF
“`

Sorting

[...orders].sort((a, b) => b.amount - a.amount) sorts largest first. The [...orders] copy matters: sort changes the array in place.

⚠️ Sorting numbers without a compare function sorts them as text: [10, 9, 100].sort() gives [10, 100, 9].
💡 Chain methods: orders.filter(…).map(…).slice(0, 5) reads like a recipe: filter, transform, take the top 5.

More: arrays for Excel users.

Practice

Run the script and add the average order amount.

📎 Practice files for this article

Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong