.find() and .findIndex()
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Finding One Element
Sometimes you don't want a list, you want one thing. You scan your team's levels looking for a Codémon strong enough to fight, and as soon as you find one, you stop looking. That's what `find` does. It takes a test callback like `filter`, but it returns a single element, not an array:
const levels = [4, 12, 9, 15];
const firstHigh = levels.find((level) => level >= 10);
console.log(firstHigh);`find` stops at the first element that passes the test. 15 also passes, but 12 came first, so 12 is returned.
const element = array.find((element) => condition);Exercise 2 — find With Objects
Real data is almost always a list of objects: a team, an inventory, a list of trainers. You rarely know where an item sits in that list, but you always know something about it: its name, its type, its id. `find` is what turns that knowledge into the object itself. It's the everyday way to look something up:
const team = [
{ name: "Aquachu", hp: 40 },
{ name: "Pyrochu", hp: 0 },
{ name: "Zap", hp: 25 }
];
const zap = team.find((cdm) => cdm.name === "Zap");
console.log(zap.hp);You get the whole object back, so every property is available. `includes` couldn't do this: it only compares whole values, and two objects are never "equal" that way.
Exercise 3 — Quiz: First Match
What does this code print?
const hp = [0, 45, 0, 30];
console.log(hp.find((value) => value > 0));45[45, 30]301Hint : Only the first element that passes is returned.
Exercise 4 — Look Up Pyrochu
Find Pyrochu in the team and print its level.
Hint : You want one object, selected by an exact comparison.
Exercise 5 — When Nothing Is Found
If no element passes the test, `find` returns `undefined`. Reading a property of `undefined` is an error, so check first:
const team = [
{ name: "Aquachu", hp: 40 },
{ name: "Zap", hp: 25 }
];
const found = team.find((cdm) => cdm.name === "Metachu");
console.log(found);
if (found === undefined) {
console.log("Metachu is not in the team");
} else {
console.log("HP: " + found.hp);
}Exercise 6 — findIndex: Where Is It?
`findIndex` works like `find` but returns the position of the first match instead of the element. It's `indexOf` with a test callback:
const team = [
{ name: "Aquachu", hp: 40 },
{ name: "Pyrochu", hp: 0 },
{ name: "Zap", hp: 25 }
];
const index = team.findIndex((cdm) => cdm.hp === 0);
console.log(index);
console.log(team[index].name);Like `indexOf`, it returns `-1` when nothing matches. The index is what you need to modify or remove the element in the original array.
Exercise 7 — Quiz: Position Found
What does this code print?
const levels = [3, 7, 12, 9];
console.log(levels.findIndex((level) => level > 10));2123trueHint : The answer is a position, not a value.
Exercise 8 — Find and Update
Find the position of the fainted Codémon, then heal it to 50 HP in the original array.
Hint : You need the position first, then use it between the brackets.
Exercise 9 — find vs findIndex
Match each element with its description.
arr.find(fn)arr.findIndex(fn)undefined-1Exercise 10 — Codédex Lookup
Write a small lookup tool for the Codédex, an array of objects.
Step 1: `describe(name)` finds the entry with that name and returns `"name: type type, level X"`. If there's no such entry, it returns `"Unknown Codémon: name"`. Step 2: print the result for `"Zap"` and for `"Florachu"`. Step 3: find the position of the first entry whose level is 10 or more, and print `"First veteran at index N"`.
Expected output:
Zap: electric type, level 4
Unknown Codémon: Florachu
First veteran at index 1const codedex = [
{ name: "Aquachu", type: "water", level: 5 },
{ name: "Metachu", type: "metal", level: 15 },
{ name: "Zap", type: "electric", level: 4 }
];
// Write your code here