find and findIndex
Locate the first matching item — or its index — in an array.
find returns the first matching element (or undefined).
findIndex returns its index (or -1 if nothing matches).
Players
Result
Important JavaScript
const players = [
{ id: 1, name: "Ava" },
{ id: 2, name: "Ben" },
{ id: 3, name: "Cara" }
];
const found = players.find((player) => player.id === 3);
// { id: 3, name: "Cara" }
const missing = players.find((player) => player.id === 99);
// undefined
const index = players.findIndex((player) => player.id === 3);
// 2
const notFound = players.findIndex((player) => player.id === 99);
// -1