JavaScript Array filter()
ဥပမာ ၁
အသက် 18 နှစ်နှင့် အထက်ရှိသော တန်ဖိုးများအားလုံး၏ အခင်းအကျင်းတစ်ခုကို ပြန်ပေးပါ-
const ages = [32, 33, 16, 40];
const result = ages.filter(checkAdult);
function checkAdult(age) {
return age >= 18;
}
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
နည်းလမ်း သည် filter()
လုပ်ဆောင်ချက်တစ်ခုမှ ပံ့ပိုးပေးသည့် စမ်းသပ်မှုတစ်ခုကို အောင်မြင်သည့် အစိတ်အပိုင်းများဖြင့် ပြည့်နေသော ခင်းကျင်းအသစ်တစ်ခုကို ဖန်တီးသည်။
နည်းလမ်း သည် filter()
ဗလာဒြပ်စင်များအတွက် လုပ်ဆောင်ချက်ကို လုပ်ဆောင်ခြင်းမရှိပါ။
နည်းလမ်းသည် မူရင်း filter()
array ကို မပြောင်းလဲပါ။
အထားအသို
array.filter(function(currentValue, index, arr), thisValue)
ကန့်သတ်ချက်များ
Parameter | Description |
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 ပံ့ပိုးမှု
filter()
ECMAScript5 (ES5) အင်္ဂါရပ်တစ်ခုဖြစ်သည်။
ES5 (JavaScript 2009) ကို ဘရောက်ဆာအားလုံးတွင် အပြည့်အဝထောက်ခံသည်-
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |
ဥပမာ ၂
သတ်မှတ်ထားသော နံပါတ်တစ်ခုထက်ကျော်လွန်သော အသက်[] ရှိ တန်ဖိုးများကို ပြန်ပေးသည်-
<p><input type="number" id="ageToCheck" value="30"></p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
const ages = [32, 33, 12, 40];
function checkAge(age) {
return age > document.getElementById("ageToCheck").value;
}
function myFunction() {
document.getElementById("demo").innerHTML = ages.filter(checkAge);
}
</script>