JavaScript Array findIndex()
ဥပမာ ၁
18 နှင့်အထက်တန်ဖိုးရှိသောပထမဒြပ်စင်ကိုရှာပါ
const ages = [3, 10, 18, 20];
ages.findIndex(checkAge);
function checkAge(age) {
return age > 18;
}
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
နည်းလမ်း သည် findIndex()
array ဒြပ်စင်တစ်ခုစီအတွက် လုပ်ဆောင်ချက်တစ်ခုကို လုပ်ဆောင်သည်။
findIndex()
နည်းလမ်းသည် စမ်းသပ်မှုတစ်ခုအောင်မြင်သော ပထမဒြပ်စင်၏ အညွှန်းကိန်း (အနေအထား) ကို ပြန်ပေးသည် ။
findIndex()
မကိုက်ညီပါက နည်းလမ်း-1 ကို ပြန်ပေးသည်
။
နည်းလမ်း သည် findIndex()
ဗလာ array အစိတ်အပိုင်းများအတွက် လုပ်ဆောင်ချက်ကို လုပ်ဆောင်ခြင်းမရှိပါ။
နည်းလမ်းသည် မူရင်း findIndex()
array ကို မပြောင်းလဲပါ။
အထားအသို
array.findIndex(function(currentValue, index, arr), thisValue)
ကန့်သတ်ချက်များ
Parameter | Description |
function() | Required. A function to be 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 ပံ့ပိုးမှု
findIndex()
ECMAScript6 (ES6) အင်္ဂါရပ်တစ်ခုဖြစ်သည်။
ES6 (JavaScript 2015) ကို ခေတ်မီဘရောက်ဆာများအားလုံးတွင် ပံ့ပိုးထားပါသည်။
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
findIndex()
Internet Explorer 11 (သို့မဟုတ်အစောပိုင်း) တွင် မပံ့ပိုးပါ။
နောက်ထပ် ဥပမာများ
ထည့်သွင်းတန်ဖိုးတစ်ခုအထက် တန်ဖိုးတစ်ခုဖြင့် ပထမဒြပ်စင်ကို ရှာပါ-
<p><input type="number" id="toCheck" value="18"></p>
<button onclick="myFunction()">Test</button>
<p>Any values above: <span id="demo"></span></p>
<script>
const numbers = [4, 12, 16, 20];
function checkValue(x) {
return x > document.getElementById("toCheck").value;
}
function myFunction() {
document.getElementById("demo").innerHTML = numbers.findIndex(checkValue);
}
</script>