v2.5.2
Giriş yap

Javascript filter() Metodu

ES5

Dizideki öğeleri belirli bir koşula göre filtreler. Filtrelenen öğeler varsa geriye döndürülür, yoksa boş bir dizi geriye döner.

Yapısı (Syntax)array.filter(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.0002360288{main}( ).../index.php:0
    20.0043444456require( '/home/prototurk.com/public_html/app/controller/category.php' ).../index.php:101
    30.0681456088require( '/home/prototurk.com/public_html/app/view/article.php' ).../category.php:34
    40.0868536080parseTemplate( ).../article.php:112
    50.0869536192preg_replace_callback ( ).../template.php:126
    60.0869536712bb_json( ).../template.php:126
    70.0874563744require( '/home/prototurk.com/public_html/app/view/article-js.php' ).../template.php:220
    Test edilen 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.0002360288{main}( ).../index.php:0
    20.0043444456require( '/home/prototurk.com/public_html/app/controller/category.php' ).../index.php:101
    30.0681456088require( '/home/prototurk.com/public_html/app/view/article.php' ).../category.php:34
    40.0868536080parseTemplate( ).../article.php:112
    50.0869536192preg_replace_callback ( ).../template.php:126
    60.0869536712bb_json( ).../template.php:126
    70.0874563744require( '/home/prototurk.com/public_html/app/view/article-js.php' ).../template.php:220
    Test edilen 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.0002360288{main}( ).../index.php:0
    20.0043444456require( '/home/prototurk.com/public_html/app/controller/category.php' ).../index.php:101
    30.0681456088require( '/home/prototurk.com/public_html/app/view/article.php' ).../category.php:34
    40.0868536080parseTemplate( ).../article.php:112
    50.0869536192preg_replace_callback ( ).../template.php:126
    60.0869536712bb_json( ).../template.php:126
    70.0874563744require( '/home/prototurk.com/public_html/app/view/article-js.php' ).../template.php:220
    Test edilen elemanın ait olduğu dizi objesi

Dönen Değer

Koşula uyan öğeleri geriye döndürür.

Örnekler

Tüm küçük öğeleri filtrelemek

Aşağıdaki kod, değeri 10'dan büyük olan öğeleri filtreler.

function isBigEnough(value) {
  return value > 10;
}

const filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
console.log(filtered); // [12, 130, 44]

Örneği Dene »

Arrow fonksiyonlarla kullanımı

Arrow fonksiyonları aynı işlemin daha kısa yazılmasını sağlar.

const filtered_with_arrow = [12, 5, 8, 130, 44].filter(value => value > 10);
console.log(filtered_with_arrow); // [12, 130, 44]

Örneği Dene »

Objelerin değerlerine göre filtrelemek

Aşağıdaki kod, dizi içindeki objelerin değerlerine göre filtreme yapar.

const users = [
	{
		name: "Tayfun Erbilen",
		age: 27
	},
	{
		name: "Mehmet Seven",
		age: 27
	},
	{
		name: "Batuhan Aydın",
		age: 28
	}
];

const filtered = users.filter(user => user.age === 27);
console.log(filtered);

Örneği Dene »

Arama yapmak

Öğeler içinde arama yapmak içinde bu metod kullanılır.

const names = ['Tayfun', 'tarkan', 'tufan', 'mehmet', 'ahmet', 'hayko'];

const search = keyword => names.filter(name => name.toLowerCase().includes(keyword.toLowerCase()));

console.log(search('ay')); // ["Tayfun", "hayko"]

Örneği Dene »

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