Javascript includes() Metodu
ES7
includes()
metodu, belirtilen değerin dizi öğelerinde olup olmadığını test eder. Metod, belirtilen değerin içinde geçmesini değil, tam olarak eşleşip eşleşmediğini kontrol eder.
Eğer aranan değer eşleşiyorsa true
eşleşmiyorsa false
döner.
Yapısı (Syntax)array.includes(valueToFind, fromIndex)
Parametreler
-
valueToFind
( ! ) 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.0005 360992 {main}( ) .../index.php:0 2 0.0051 445160 require( '/home/prototurk.com/public_html/app/controller/category.php' ) .../index.php:101 3 0.0085 456792 require( '/home/prototurk.com/public_html/app/view/article.php' ) .../category.php:34 4 0.0162 536784 parseTemplate( ) .../article.php:112 5 0.0163 536896 preg_replace_callback ( ) .../template.php:126 6 0.0163 537432 bb_json( ) .../template.php:126 7 0.0165 563664 require( '/home/prototurk.com/public_html/app/view/article-js.php' ) .../template.php:220 Aranacak değer -
fromIndex
( ! ) 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.0005 360992 {main}( ) .../index.php:0 2 0.0051 445160 require( '/home/prototurk.com/public_html/app/controller/category.php' ) .../index.php:101 3 0.0085 456792 require( '/home/prototurk.com/public_html/app/view/article.php' ) .../category.php:34 4 0.0162 536784 parseTemplate( ) .../article.php:112 5 0.0163 536896 preg_replace_callback ( ) .../template.php:126 6 0.0163 537432 bb_json( ) .../template.php:126 7 0.0165 563664 require( '/home/prototurk.com/public_html/app/view/article-js.php' ) .../template.php:220 Aramanın başlayacağı indis değeri
Dönen Değer
Eğer dizi içinde aranan değer varsa true
yoksa false
döndürür.
NOT
Metod, büyük/küçük harfe duyarlıdır. O yüzden
prototurk
ile Prototurk
eşleşmeyecektir. Örnekler
En basit örneklerle açıklamak gerekirse
console.log( [1, 2, 3].includes(2) ) // true
console.log( [1, 2, 3].includes(4) ) // false
console.log( [1, 2, 3].includes(3, 3) ) // false
console.log( [1, 2, 3].includes(3, -1) ) // true
console.log( [1, 2, NaN].includes(NaN) ) // true
fromIndex
parametresi
Eğer bu parametrenin değeri dizi uzunluğundan fazla olursa, metod eşleşme aramadan false
döndürecektir.
let arr = ['a', 'b', 'c']
arr.includes('c', 3) // false
arr.includes('c', 100) // false
fromIndex
0'dan küçük ise
Eğer fromIndex parametresini sıfırdan küçük değer verirseniz mevcut dizi uzunluğu ile verdiğiniz değer toplanır ve ona göre eşleşme aranır.
// örnek bir anlatım
// array uzunluğu = 3
// fromIndex = -100
// indis başlangıcı = 3 + (-100) = -97
let arr = ['a', 'b', 'c']
arr.includes('a', -100) // true
arr.includes('b', -100) // true
arr.includes('c', -100) // true
arr.includes('a', -2) // false - çünkü (3 + (-2)) = 1 olacağı için 1. indiste değer b olduğundan false döner