v2.5.2
Giriş yap

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
    #TimeMemoryFunctionLocation
    10.0003360272{main}( ).../index.php:0
    20.0047444440require( '/home/prototurk.com/public_html/app/controller/category.php' ).../index.php:101
    30.0081456064require( '/home/prototurk.com/public_html/app/view/article.php' ).../category.php:34
    40.0165535992parseTemplate( ).../article.php:112
    50.0165536104preg_replace_callback ( ).../template.php:126
    60.0165536624bb_json( ).../template.php:126
    70.0167562864require( '/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
    #TimeMemoryFunctionLocation
    10.0003360272{main}( ).../index.php:0
    20.0047444440require( '/home/prototurk.com/public_html/app/controller/category.php' ).../index.php:101
    30.0081456064require( '/home/prototurk.com/public_html/app/view/article.php' ).../category.php:34
    40.0165535992parseTemplate( ).../article.php:112
    50.0165536104preg_replace_callback ( ).../template.php:126
    60.0165536624bb_json( ).../template.php:126
    70.0167562864require( '/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
    #TimeMemoryFunctionLocation
    10.0003360272{main}( ).../index.php:0
    20.0047444440require( '/home/prototurk.com/public_html/app/controller/category.php' ).../index.php:101
    30.0081456064require( '/home/prototurk.com/public_html/app/view/article.php' ).../category.php:34
    40.0165535992parseTemplate( ).../article.php:112
    50.0165536104preg_replace_callback ( ).../template.php:126
    60.0165536624bb_json( ).../template.php:126
    70.0167562864require( '/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.

Ö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 }

Örneği Dene »

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' ));

Örneği Dene »

tayfunerbilen
1258 gün önce eklendi - 3498 kez görüntülendi.
Github'da Düzenle
Önceki push() Sonraki includes()