Her biri için JavaScript Dizisi()
örnek 1
Meyvelerdeki her öğe için bir işlev çağırır:
const fruits = ["apple", "orange", "cherry"];
fruits.forEach(myFunction);
Tanım ve Kullanım
Yöntem forEach()
, dizideki her öğe için bir işlev çağırır.
Yöntem forEach()
, boş öğeler için yürütülmez.
Sözdizimi
array.forEach(function(currentValue, index, arr), thisValue)
parametreler
function() | Required. A function to run for each array element. |
currentValue | Required. The value of the current element. |
index | Optional. The index of the current element. |
arr | Optional. The array of the current element. |
thisValue | Optional. Default undefined .A value passed to the function as its this value. |
Geri dönüş değeri
Tanımsız |
Tarayıcı Desteği
forEach()
bir ECMAScript5 (ES5) özelliğidir.
ES5 (JavaScript 2009) tüm tarayıcılarda tam olarak desteklenir:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |
Daha fazla örnek
Toplamı hesaplayın:
let sum = 0;
const numbers = [65, 44, 12, 4];
numbers.forEach(myFunction);
function myFunction(item) {
sum += item;
}
Her elemanı çarpın:
const numbers = [65, 44, 12, 4];
numbers.forEach(myFunction)
function
myFunction(item, index, arr) {
arr[index] = item * 10;
}