जावास्क्रिप्ट ऐरे खोजें ()
उदाहरण 1
18 से अधिक मान वाले पहले तत्व का मान ज्ञात कीजिए:
const ages = [3, 10, 18, 20];
function checkAge(age) {
return age > 18;
}
function myFunction() {
document.getElementById("demo").innerHTML = ages.find(checkAge);
}
परिभाषा और उपयोग
find()
विधि पहले तत्व का मान लौटाती है जो एक परीक्षण पास करता है ।
find()
विधि प्रत्येक सरणी तत्व के लिए एक फ़ंक्शन निष्पादित करती है ।
यदि कोई तत्व नहीं मिलता है तो find()
विधि फिर से चालू हो जाती है।undefined
विधि खाली तत्वों के find()
लिए फ़ंक्शन निष्पादित नहीं करती है।
विधि मूल सरणी को find()
नहीं बदलती है।
वाक्य - विन्यास
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 । |
ब्राउज़र समर्थन
find()
एक ECMAScript6 (ES6) फीचर है।
ES6 (जावास्क्रिप्ट 2015) सभी आधुनिक ब्राउज़रों में समर्थित है:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
find()
Internet Explorer 11 (या पुराने) में समर्थित नहीं है।
उदाहरण 2
एक विशिष्ट संख्या से ऊपर के मान वाले पहले तत्व का मान ज्ञात कीजिए:
<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>