JavaScript Array ရှာရန်()
ဥပမာ ၁
18 အထက်တန်ဖိုးဖြင့် ပထမဒြပ်စင်၏တန်ဖိုးကို ရှာပါ-
const ages = [3, 10, 18, 20];
function checkAge(age) {
return age > 18;
}
function myFunction() {
document.getElementById("demo").innerHTML = ages.find(checkAge);
}
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
နည်းလမ်းသည် စမ်းသပ်မှုတစ်ခုအောင်မြင်သော ပထမဒြပ်စင် ၏ find()
တန်ဖိုးကို ပြန်ပေးသည်။
နည်းလမ်း သည် find()
array ဒြပ်စင်တစ်ခုစီအတွက် လုပ်ဆောင်ချက်တစ်ခုကို လုပ်ဆောင်သည်။
အစိတ်အပိုင်းများမတွေ့ပါက နည်းလမ်း find()
သည် ပြန် ဖွင့်သည်။undefined
နည်းလမ်း သည် find()
ဗလာဒြပ်စင်များအတွက် လုပ်ဆောင်ချက်ကို လုပ်ဆောင်ခြင်းမရှိပါ။
နည်းလမ်းသည် မူရင်း find()
array ကို မပြောင်းလဲပါ။
အထားအသို
array.find(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. |
တန်ဖိုးကို ပြန်ပေးသည်။
ရိုက်ပါ။ | ဖော်ပြချက် |
တန်ဖိုးတစ်ခု |
စမ်းသပ်မှုအောင်မြင်သောပထမဒြပ်စင်၏တန်ဖိုး။ မဟုတ်ရင် ပြန်လာမှာပါ undefined ။ |
Browser ပံ့ပိုးမှု
find()
ECMAScript6 (ES6) အင်္ဂါရပ်တစ်ခုဖြစ်သည်။
ES6 (JavaScript 2015) ကို ခေတ်မီဘရောက်ဆာများအားလုံးတွင် ပံ့ပိုးထားပါသည်။
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
find()
Internet Explorer 11 (သို့မဟုတ်အစောပိုင်း) တွင် မပံ့ပိုးပါ။
ဥပမာ ၂
သတ်မှတ်ထားသော နံပါတ်တစ်ခုအထက်တန်ဖိုးရှိသော ပထမဒြပ်စင်၏တန်ဖိုးကို ရှာပါ-
<p><input type="number" id="ageToCheck" value="18"></p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
const ages = [4, 12, 16, 20];
function checkAge(age) {
return age > document.getElementById("ageToCheck").value;
}
function myFunction() {
document.getElementById("demo").innerHTML = ages.find(checkAge);
}
</script>