v2.5.2
Giriş yap

Javascript every() Metodu

ES5

Dizideki tüm öğelerin belirtilen koşula uygun olup olmadığını test eder. Testi geçemeyen ilk öğede metod geriye false döndürür ve işlemi durdurur. Eğer tüm öğeler testi geçerse geriye true döner.

Yapısı (Syntax)array.every(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.0001360896{main}( ).../index.php:0
    20.0032445064require( '/home/prototurk.com/public_html/app/controller/category.php' ).../index.php:101
    30.0059456688require( '/home/prototurk.com/public_html/app/view/article.php' ).../category.php:34
    40.0126536680parseTemplate( ).../article.php:112
    50.0126536792preg_replace_callback ( ).../template.php:126
    60.0126537312bb_json( ).../template.php:126
    70.0128566880require( '/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.0001360896{main}( ).../index.php:0
    20.0032445064require( '/home/prototurk.com/public_html/app/controller/category.php' ).../index.php:101
    30.0059456688require( '/home/prototurk.com/public_html/app/view/article.php' ).../category.php:34
    40.0126536680parseTemplate( ).../article.php:112
    50.0126536792preg_replace_callback ( ).../template.php:126
    60.0126537312bb_json( ).../template.php:126
    70.0128566880require( '/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.0001360896{main}( ).../index.php:0
    20.0032445064require( '/home/prototurk.com/public_html/app/controller/category.php' ).../index.php:101
    30.0059456688require( '/home/prototurk.com/public_html/app/view/article.php' ).../category.php:34
    40.0126536680parseTemplate( ).../article.php:112
    50.0126536792preg_replace_callback ( ).../template.php:126
    60.0126537312bb_json( ).../template.php:126
    70.0128566880require( '/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

Dizideki tüm öğeler testi geçerse true, aksi halde false döner.

Örnekler

Tüm öğelerin değerini test etme

Aşağıdaki kod, dizideki tüm öğelerin 10'dan büyük olup olmadığını test eder.

function isBigEnough(el) {
  return el >= 10;
}
[12, 5, 8, 130, 44].every(isBigEnough);   // false
[12, 54, 18, 130, 44].every(isBigEnough); // true

Örneği Dene »

Arrow fonksiyonlarla kullanımı

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

[12, 5, 8, 130, 44].every(x => x >= 10);   // false
[12, 54, 18, 130, 44].every(x => x >= 10); // true

Örneği Dene »

Objeleri test etmek

Metod parametre olarak sadece dizi değil bir değerde alabilir. Aşağıdaki kod, bir değer ve dizilerin birleşimini gösterir.

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

console.log(users.every(el => el.age === 27));

Örneği Dene »

Diğer parametrelerine bir örnek

Aşağıdaki kod, 2. ve 3. parametrelerinin kullanımını gösteren bir örnektir.

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

function isSameAge(el, index, arr) {
	if (index === 0 || index === arr.length - 1)
		return true;
	else
		return (el.age === arr[index - 1].age);
}

console.log(users.every(isSameAge));
// Çıktı: true
// çünkü ilk ve son öğe ise doğrudan true döndürdük, bu yüzden son öğeyi
// kontrol etmedi ve bütün öğelerin yaşının eşit olduğunu varsaydı.

Örneği Dene »

Öğelerin değiştirilmesi

Eğer test sırasında diziyi güncellerseniz, yeni bir öğe eklerseniz ya da bir öğeyi silerseniz nasıl işleyeceğine dair bir örnek.

// Öğeleri güncellemek
let arr = [1, 2, 3, 4];
let result = arr.every( (elem, index, arr) => {
  arr[index + 2] -= 2; // her test ettiğinde 2 sonraki öğesinin indisini 2 azaltıyoruz
  console.log(`[${arr}][${index}] -> ${elem}`)
  return elem < 2
});

console.log(result);
// Çıktı
// "[1,2,1,4][0] -> 1"
// "[1,2,1,2][1] -> 2"
//  false


// Yeni öğe eklemek
arr = [1, 2, 3];
result = arr.every( (elem, index, arr) => {
  arr.push('new')
  console.log(`[${arr}][${index}] -> ${elem}`)
  return elem < 4
});

console.log(result)
// Çıktı
// "[1,2,3,new][0] -> 1"
// "[1,2,3,new,new][1] -> 2"
// "[1,2,3,new,new,new][2] -> 3"
// true

// Öğeleri silmek
arr = [1, 2, 3, 4];
result = arr.every( (elem, index, arr) => {
  arr.pop()
  console.log(`[${arr}][${index}] -> ${elem}`)
  return elem >= 1 && elem < 3;
});

console.log(result);
// Çıktı
// "[1,2,3][0] -> 1"
// "[1,2][1] -> 2"
// true

Örneği Dene »

tayfunerbilen
1350 gün önce eklendi - 3413 kez görüntülendi.
Github'da Düzenle
Önceki concat() Sonraki filter()