Javascript find() Metodu
ES6
find() metodu, dizi içinde belirlediğimiz koşula uygun olan ilk elemanı seçmeye yarar.
Eğer koşula uygun eleman bulunamazsa, geriye undefined döndürür.
Yapısı (Syntax)array.find(function(currentValue, index, arr),thisValue)
Parametreler
-
currentValue
( ! ) Notice: Undefined property: stdClass::$type in /home/prototurk.com/public_html/app/view/article-js.php on line 64 Call Stack # Time Memory Function Location 1 0.0001 362216 {main}( ) .../index.php:0 2 0.0353 446280 require( '/home/prototurk.com/public_html/app/controller/category.php' ) .../index.php:101 3 0.0373 457904 require( '/home/prototurk.com/public_html/app/view/article.php' ) .../category.php:34 4 0.0414 537832 parseTemplate( ) .../article.php:112 5 0.0414 537944 preg_replace_callback ( ) .../template.php:126 6 0.0414 538464 bb_json( ) .../template.php:126 7 0.0415 564704 require( '/home/prototurk.com/public_html/app/view/article-js.php' ) .../template.php:220 İşlemden geçen elemanın değeri -
index
( ! ) Notice: Undefined property: stdClass::$type in /home/prototurk.com/public_html/app/view/article-js.php on line 64 Call Stack # Time Memory Function Location 1 0.0001 362216 {main}( ) .../index.php:0 2 0.0353 446280 require( '/home/prototurk.com/public_html/app/controller/category.php' ) .../index.php:101 3 0.0373 457904 require( '/home/prototurk.com/public_html/app/view/article.php' ) .../category.php:34 4 0.0414 537832 parseTemplate( ) .../article.php:112 5 0.0414 537944 preg_replace_callback ( ) .../template.php:126 6 0.0414 538464 bb_json( ) .../template.php:126 7 0.0415 564704 require( '/home/prototurk.com/public_html/app/view/article-js.php' ) .../template.php:220 İşlemden geçen elemanın indis değeri -
arr
( ! ) Notice: Undefined property: stdClass::$type in /home/prototurk.com/public_html/app/view/article-js.php on line 64 Call Stack # Time Memory Function Location 1 0.0001 362216 {main}( ) .../index.php:0 2 0.0353 446280 require( '/home/prototurk.com/public_html/app/controller/category.php' ) .../index.php:101 3 0.0373 457904 require( '/home/prototurk.com/public_html/app/view/article.php' ) .../category.php:34 4 0.0414 537832 parseTemplate( ) .../article.php:112 5 0.0414 537944 preg_replace_callback ( ) .../template.php:126 6 0.0414 538464 bb_json( ) .../template.php:126 7 0.0415 564704 require( '/home/prototurk.com/public_html/app/view/article-js.php' ) .../template.php:220 İşlemden geçen elemanın ait olduğu dizi
Dönen Değer
Koşula uyan ilk dizi elemanını döndürür. Hiçbiri koşula uymazsa geriye undefined döndürür.
NOT
Bu metod varsayılan diziyi değiştirmez ve test edilen dizi boş ise çalışmaz.
Eğer bulunan ilk dizi elemanının indis değerini almak isterseniz findIndex() metoduna bakın.
Eğer dizide geçen bir değeri bulmak istiyorsanız includes() metoduna bakın.
Eğer herhangi bir öğenin koşulu karşılayıp karşılamadığını test etmek istiyorsanız some() metoduna bakın.
Eğer bulunan ilk dizi elemanının indis değerini almak isterseniz findIndex() metoduna bakın.
Eğer dizide geçen bir değeri bulmak istiyorsanız includes() metoduna bakın.
Eğer herhangi bir öğenin koşulu karşılayıp karşılamadığını test etmek istiyorsanız some() metoduna bakın.
Örnekler
Nesnenin özelliğine göre dizi öğesini bulma
Aşağıdaki kod, inventory dizisinde name özelliği cherries olan öğeyi döndürecektir.
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 }
Aynı işlemin arrow fonksiyon ve destructuring yöntemi ile kullanımı
const inventory = [
{name: 'apples', quantity: 2},
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
];
// Bunu arrow fonsiyonu ve destructuring ile bulmak isteseydiniz
console.log(inventory.find( ({ name }) => name === 'cherries' ));