JavaScript配列find

条件を満たす最初の要素を返します。

構文

arr.find(callbackFn)

使用例

下記の値を入力するとサンプルに即時反映されます。

const
users
id
name
user
find
console
const users = [
  { id: 1, name: "Alice" },
  { id: 2, name: "Bob" }
];
const user = users.find((u) => u.id === 2);
console.log(user?.name);   // "Bob"