مصفوفة JavaScript forEach ()
مثال 1
يستدعي وظيفة لكل عنصر في الفاكهة:
const fruits = ["apple", "orange", "cherry"];
fruits.forEach(myFunction);
التعريف والاستخدام
تستدعي forEach()
الطريقة وظيفة لكل عنصر في المصفوفة.
لم يتم forEach()
تنفيذ الطريقة للعناصر الفارغة.
بناء الجملة
array.forEach(function(currentValue, index, arr), thisValue)
المعلمات
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. |
قيمة الإرجاع
غير معرف |
دعم المتصفح
forEach()
هي إحدى ميزات ECMAScript5 (ES5).
ES5 (JavaScript 2009) مدعوم بالكامل في جميع المتصفحات:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |
مزيد من الأمثلة
احسب المجموع:
let sum = 0;
const numbers = [65, 44, 12, 4];
numbers.forEach(myFunction);
function myFunction(item) {
sum += item;
}
اضرب كل عنصر:
const numbers = [65, 44, 12, 4];
numbers.forEach(myFunction)
function
myFunction(item, index, arr) {
arr[index] = item * 10;
}