戻る

find()メソッド
配列の中から特定の条件を満たす最初の要素を見つけて返す関数です。

[サンプル]
copy
const ary1 = [10, 20, 30, 40, 50];

//4の剰余が0のみ(4で割り切れる数字のみ)を条件とした最初の値を返します
const ary2 = ary1.find(function(ary1) 
{
	return ary1 % 4 === 0;
});

console.log(ary1);//[10, 20, 30, 40, 50]
console.log(ary2);//20(最初に一致した結果のみを返すため)
配列の中から特定の条件を満たす最初の要素を見つけて返す関数です。
要素が見つからない場合はundefinedを返します。
コールバック関数がtrueを返す最初の要素を見つけると、その要素を返します。
それ以降の要素は処理されません。

copy
const ary3 = [
{id:0, animal:"cat"},
{id:1, animal:"mouse"},
{id:2, animal:"dog"}
];
let searchId =2;
const result = ary3.find(ary => ary.id === searchId);

console.log(result);//id:2, animal:"dog"
ary => ary.id === searchId
ary配列の各要素を順番に受け取る仮の変数名です。
=>関数の定義を表します。
ary.id === searchIdaryのidプロパティがsearchId変数と等しいか?を比較判定する条件式です。

find()メソッドは、配列の各要素に対してこのコールバック関数を適用し、
trueを返す要素が見つかった時点でその要素を返します。


戻る
back

find() method
This function finds and returns the first element in an array that satisfies a specific condition.

[sample]
copy
const ary1 = [10, 20, 30, 40, 50];

//Returns the first value that is divisible by 4 (only numbers that are divisible by 4)
const ary2 = ary1.find(function(ary1) 
{
	return ary1 % 4 === 0;
});

console.log(ary1);//[10, 20, 30, 40, 50]
console.log(ary2);//20(To return only the first matching result)
This function finds and returns the first element in an array that meets a specific condition.
If the element is not found, it returns undefined.
When it finds the first element for which the callback function returns true, it returns that element.
Subsequent elements are not processed.

copy
const ary3 = [
{id:0, animal:"cat"},
{id:1, animal:"mouse"},
{id:2, animal:"dog"}
];
let searchId =2;
const result = ary3.find(ary => ary.id === searchId);

console.log(result);//id:2, animal:"dog"
ary => ary.id === searchId
aryThis is a temporary variable name that receives each element of the array in order.
=>This represents the function definition.
ary.id === searchIdThis is a conditional expression that compares whether the id property of ary is equal to the searchId variable.

The find() method applies this callback function to each element in the array, and
returns the element if one is found that returns true.



back



著作権情報
ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。
ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、
または公開したりすることはできません。
当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、
その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について
一切の責任を負わないものとします。