The conveyor at the fish factory
map, filter, reduce, find, some, method chains, plus forEach, findIndex, flat/flatMap and the sneaky sort.
const weights = [5, 1, 4];
const doubled = weights.map((w) => w * 2);
console.log(doubled);
console.log(weights);map does the same thing to each item and returns a new array.The three main stations:
- `map(fn)` — transforms each item: an array of the same length;
- `filter(fn)` — keeps only the items for which
fnreturnedtrue; - `reduce(fn, start)` — folds the whole array into one value: a sum, a maximum, an object.
All of them take a function — usually an arrow — and call it for each item. The original array doesn't change.
const fish = [
{ name: "tuna", weight: 5, fresh: true },
{ name: "sprat", weight: 1, fresh: false },
{ name: "salmon", weight: 4, fresh: true },
];
console.log(fish.filter((f) => f.fresh));
console.log(fish.map((f) => f.name));
console.log(fish.reduce((total, f) => total + f.weight, 0));Stations can be joined into a chain: each method returns a new array, and the next one works on it. It reads top to bottom, like a fish's route through the factory.
const fish = [
{ name: "tuna", weight: 5, fresh: true },
{ name: "sprat", weight: 1, fresh: false },
{ name: "salmon", weight: 4, fresh: true },
];
const freshNames = fish
.filter((f) => f.fresh)
.map((f) => f.name.toUpperCase());
console.log(freshNames);Three more useful stations:
find(fn)— the first matching item (or undefined);some(fn)— whether at least one matches;every(fn)— whether all match.
Two more stations you'll use every day:
forEach(fn)— just do something with each item: print it, send it, save it. It returns nothing (undefined), so it can't join a chain;findIndex(fn)— like find, but returns the position of the matching item, or-1if there's none, just likeindexOf.
The callback gets not only the item but also its index as the second argument: (name, i) => ….
const queue = ["tuna", "sprat", "salmon"];
queue.forEach((name, i) => {
console.log("Place " + i + ": " + name);
});
console.log(queue.findIndex((name) => name === "sprat"));
console.log(queue.findIndex((name) => name === "shark"));forEach prints, findIndex finds a place in the queue.Fish arrive in crates, and crates are arrays inside an array. flat() tips them out onto a single belt. And flatMap(fn) is a map whose result gets tipped out straight away: handy when one item turns into several values (or none).
const crates = [["tuna", "sprat"], ["salmon"], []];
console.log(crates.flat());
const orders = [
{ cat: "Byte", fish: ["tuna", "salmon"] },
{ cat: "Murka", fish: ["sprat"] },
];
console.log(orders.map((o) => o.fish));
console.log(orders.flatMap((o) => o.fish));map returned the crates; flatMap returned all the fish on one belt.And the sorting station — sort. To sort numbers it needs a compare function, (a, b) => a - b: a negative result puts a first, a positive one puts it later. For “biggest first”, use (a, b) => b - a.
const weights = [10, 9, 1, 100];
console.log([...weights].sort());
console.log([...weights].sort((a, b) => a - b));
console.log(weights);Pop quiz
Mistakes are fine: keep trying until you find the right answer, and Byte explains why it's right.