जावास्क्रिप्ट ऐरे फाइंडइंडेक्स ()
उदाहरण 1
18 से अधिक मान वाला पहला तत्व खोजें:
const ages = [3, 10, 18, 20];
ages.findIndex(checkAge);
function checkAge(age) {
return age > 18;
}
परिभाषा और उपयोग
findIndex()
विधि प्रत्येक सरणी तत्व के लिए एक फ़ंक्शन निष्पादित करती है ।
findIndex()
विधि पहले तत्व का सूचकांक (स्थिति) लौटाती है जो एक परीक्षण पास करता है ।
यदि findIndex()
कोई मिलान नहीं मिलता है तो विधि -1 लौटाती है।
findIndex()
विधि खाली सरणी तत्वों के लिए फ़ंक्शन निष्पादित नहीं करती है ।
विधि मूल सरणी को findIndex()
नहीं बदलती है।
वाक्य - विन्यास
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. |
प्रतिलाभ की मात्रा
प्रकार | विवरण |
एक संख्या |
परीक्षण पास करने वाले पहले तत्व का सूचकांक। अन्यथा -1। |
ब्राउज़र समर्थन
findIndex()
एक ECMAScript6 (ES6) फीचर है।
ES6 (जावास्क्रिप्ट 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>