JavaScript theorytheory 0/50 · 0%
Data · easy

6. Arrays and iteration

map, filter, reduce and when to use each.

Arrays are ordered, resizable lists. `map` transforms each element, `filter` keeps some, `reduce` folds to a single value, and `for…of` is best when you need `await` or `break`.

const fees = [21000, 45000, 60000];
const gwei = fees.map((f) => f / 1e9);
const big = fees.filter((f) => f > 30000);
const total = fees.reduce((a, b) => a + b, 0);

`map`, `filter` and `slice` return new arrays; `push`, `sort` and `splice` mutate in place.

Check your understanding

  1. 1. Which method folds an array to one value?

  2. 2. Which of these mutates the original array?

  3. 3. Which loop lets you `await` inside cleanly?