10
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【JavaScript・初学者】Reactでよく使われる頻出記法 / findについて

Last updated at Posted at 2023-11-14

はじめに

Reactを学習していましたが、JavaScriptの記法についてフワッとしている部分がよくあったので、簡単ですが今回はfindについて少し調べました。

findの使い方

配列から条件に一致した最初の値を返すメソッドです。
その為、1つしか値を取得できないことを覚えておきましょう。

const numbers = [1,2,3,4,5]
const foundNumber = numbers.find((number) => {
  return number > 3
})
console.log(foundNumber); //4

オブジェクト配列にも使うことができます。

const inventory = [
  { name: "apples", quantity: 2 },
  { name: "bananas", quantity: 0 },
  { name: "cherries", quantity: 5 },
];

function isCherries(fruit) {
  return fruit.name === "cherries";
}

console.log(inventory.find(isCherries));
// { name: 'cherries', quantity: 5 }
10
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?