JavaScript Array လျှော့ချ()
ဥပမာများ
အခင်းတစ်ခုတွင် ဂဏန်းအားလုံးကို နုတ်ပါ-
const numbers = [175, 50, 25];
document.getElementById("demo").innerHTML
= numbers.reduce(myFunc);
function myFunc(total, num) {
return total - num;
}
နံပါတ်များအားလုံးကို ဝိုင်းပြီး ရလဒ်ကို ပြပါ-
const numbers = [15.5, 2.3, 1.1, 4.7];
document.getElementById("demo").innerHTML = numbers.reduce(getSum, 0);
function getSum(total, num) {
return total + Math.round(num);
}
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
နည်းလမ်း သည် reduce()
array element အတွက် reducer function ကို လုပ်ဆောင်သည်။
reduce()
နည်းလမ်းသည် တန်ဖိုးတစ်ခုတည်းကို ပြန်ပေးသည်- လုပ်ဆောင်ချက်၏ စုစည်းမှုရလဒ် ။
နည်းလမ်း သည် reduce()
ဗလာ array အစိတ်အပိုင်းများအတွက် လုပ်ဆောင်ချက်ကို လုပ်ဆောင်ခြင်းမရှိပါ။
နည်းလမ်းသည် မူရင်း reduce()
array ကို မပြောင်းလဲပါ။
ကိုလည်းကြည့်ပါ-
အထားအသို
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
ကန့်သတ်ချက်များ
Parameter | Description | ||||||||
function() | Required. A function to be run for each element in the array. |
||||||||
Reducer function parameters:
| |||||||||
initialValue | Optional. A value to be passed to the function as the initial value. |
တန်ဖိုးကို ပြန်ပေးသည်။
ပြန်ခေါ်သည့်လုပ်ဆောင်ချက်၏ နောက်ဆုံးခေါ်ဆိုမှုမှ စုဆောင်းထားသောရလဒ်။ |
Browser ပံ့ပိုးမှု
reduce()
ECMAScript5 (ES5) အင်္ဂါရပ်တစ်ခုဖြစ်သည်။
ES5 (JavaScript 2009) ကို ဘရောက်ဆာအားလုံးတွင် အပြည့်အဝထောက်ခံသည်-
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |