Es6-array-find
提供:Dev Guides
ES6-Array.find
この関数は、指定されたテスト関数を満たす配列の最初の要素の値を返します。 それ以外の場合、undefinedが返されます。
構文
以下は、配列メソッド* find()の構文です。ここで、 *thisArg は、 callback を実行するときにthisとして使用するオプションのオブジェクトであり、callbackは、配列の各値で実行される関数で、次のように引数-
- element -配列で処理されている現在の要素。
- index -これはオプションです。配列で処理されている現在の要素のインデックスを参照します。
- array -これはオプションです。 findが呼び出された配列。
arr.find(callback(element[, index[, array]])[, thisArg])
例
<script>
//find
const products = [{name:'Books',quantity:10},
{name:'Pen',quantity:20},
{name:"Books",quantity:30}
]
console.log( products.find(p=>p.name==="Books"))
</script>
上記のコードの出力は以下のようになります-
{name: "Books", quantity: 10}