JavaScript Array forEach()
ဥပမာ ၁
အသီးအနှံများရှိ ဒြပ်စင်တစ်ခုစီအတွက် လုပ်ဆောင်ချက်တစ်ခုကို ခေါ်ဆိုသည်-
const fruits = ["apple", "orange", "cherry"];
fruits.forEach(myFunction);
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
forEach()
နည်းလမ်းသည် array တစ်ခုရှိ element တစ်ခုစီအတွက် function တစ်ခုစီကိုခေါ်ဆိုသည် ။
ဗလာဒြပ်စင် များအတွက် 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. |
တန်ဖိုးကို ပြန်ပေးသည်။
သတ်မှတ်မထားသော |
Browser ပံ့ပိုးမှု
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;
}