JavaScript में Array Search Methods
JavaScript ऐरे (array) में तत्वों (elements) को खोजने के लिए इन-बिल्ट मेथड्स प्रदान करता है। ये टूल्स आपको उनकी स्थिति (position) खोजने या यह जाँचने में मदद करते हैं कि कोई वैल्यू मौजूद है या नहीं।
यहाँ मुख्य मेथड्स दिए गए हैं जिन्हें आपको जानना चाहिए:
indexOf() किसी विशिष्ट तत्व का पहला इंडेक्स (index) ढूँढता है। यदि तत्व मौजूद नहीं है, तो यह -1 लौटाता है। Example:
const fruits = ["Apple", "Banana", "Mango", "Banana"];fruits.indexOf("Banana"); // Returns 1lastIndexOf() किसी विशिष्ट तत्व का अंतिम इंडेक्स ढूँढता है। यदि तत्व मौजूद नहीं है, तो यह -1 लौटाता है। Example:
const fruits = ["Apple", "Banana", "Mango", "Banana"];fruits.lastIndexOf("Banana"); // Returns 3includes() जाँचता है कि क्या कोई तत्व आपके ऐरे में मौजूद है। यह true या false लौटाता है। Example:
const fruits = ["Apple", "Banana", "Mango"];fruits.includes("Mango"); // Returns truefruits.includes("Orange"); // Returns falsefind() आपकी शर्त (condition) को पूरा करने वाला पहला तत्व लौटाता है। यदि कोई मैच नहीं मिलता है, तो यह undefined लौटाता है। Example:
const numbers = [5, 12, 8, 20];const result = numbers.find(num => num > 10); // Returns 12findIndex() आपकी शर्त को पूरा करने वाले पहले तत्व का इंडेक्स लौटाता है। यदि कोई मैच नहीं मिलता है, तो यह -1 लौटाता है। Example:
const numbers = [5, 12, 8, 20];const index = numbers.findIndex(num => num > 10); // Returns 1findLast() आपकी शर्त को पूरा करने वाला अंतिम तत्व लौटाता है। यह ऐरे के अंत से खोजता है। Example:
const numbers = [5, 12, 8, 20];const result = numbers.findLast(num => num > 10); // Returns 20findLastIndex() आपकी शर्त को पूरा करने वाले अंतिम तत्व का इंडेक्स लौटाता है। Example:
const numbers = [5, 12, 8, 20];const index = numbers.findLastIndex(num => num > 10); // Returns 3
सारांश:
• indexOf() पहला इंडेक्स प्राप्त करता है। • lastIndexOf() अंतिम इंडेक्स प्राप्त करता है। • includes() मौजूदगी की जाँच करता है। • find() पहला मैचिंग वैल्यू प्राप्त करता है। • findIndex() पहला मैचिंग इंडेक्स प्राप्त करता है। • findLast() अंतिम मैचिंग वैल्यू प्राप्त करता है। • findLastIndex() अंतिम मैचिंग इंडेक्स प्राप्त करता है।
Source: https://www.w3schools.com/js/js_array_search.asp
Full post: https://dev.to/kamalesh_ar_6252544786997/array-search-methods-in-javascript-23mk
