JavaScript-இல் Array தேடல் முறைகள் (Array Search Methods in JavaScript)
JavaScript ஒரு array-இல் உள்ள உறுப்புகளைக் கண்டறிய உள்ளமைக்கப்பட்ட (built-in) முறைகளை வழங்குகிறது. இந்தத் கருவிகள் ஒரு உறுப்பின் இருப்பிடத்தைக் கண்டறிய அல்லது ஒரு மதிப்பு இருக்கிறதா என்பதைச் சரிபார்க்க உதவுகின்றன.
நீங்கள் தெரிந்து கொள்ள வேண்டிய முக்கிய முறைகள் இங்கே:
indexOf() ஒரு குறிப்பிட்ட உறுப்பின் முதல் index-ஐக் கண்டறியும். அந்த உறுப்பு இல்லையென்றால் -1 என்பதைத் தரும். Example: const fruits = ["Apple", "Banana", "Mango", "Banana"]; fruits.indexOf("Banana"); // Returns 1
lastIndexOf() ஒரு குறிப்பிட்ட உறுப்பின் கடைசி index-ஐக் கண்டறியும். அந்த உறுப்பு இல்லையென்றால் -1 என்பதைத் தரும். Example: const fruits = ["Apple", "Banana", "Mango", "Banana"]; fruits.lastIndexOf("Banana"); // Returns 3
includes() உங்கள் array-இல் ஒரு உறுப்பு இருக்கிறதா என்பதைச் சரிபார்க்கும். இது true அல்லது false என்பதைத் தரும். Example: const fruits = ["Apple", "Banana", "Mango"]; fruits.includes("Mango"); // Returns true fruits.includes("Orange"); // Returns false
find() உங்கள் நிபந்தனைக்கு (condition) பொருந்தும் முதல் உறுப்பைத் தரும். பொருத்தமான ஒன்று இல்லையென்றால் undefined என்பதைத் தரும். Example: const numbers = [5, 12, 8, 20]; const result = numbers.find(num => num > 10); // Returns 12
findIndex() உங்கள் நிபந்தனைக்கு பொருந்தும் முதல் உறுப்பின் index-ஐத் தரும். பொருத்தமான ஒன்று இல்லையென்றால் -1 என்பதைத் தரும். Example: const numbers = [5, 12, 8, 20]; const index = numbers.findIndex(num => num > 10); // Returns 1
findLast() உங்கள் நிபந்தனைக்கு பொருந்தும் கடைசி உறுப்பைத் தரும். இது array-இன் இறுதியிலிருந்து தேடும். Example: const numbers = [5, 12, 8, 20]; const result = numbers.findLast(num => num > 10); // Returns 20
findLastIndex() உங்கள் நிபந்தனைக்கு பொருந்தும் கடைசி உறுப்பின் index-ஐத் தரும். Example: const numbers = [5, 12, 8, 20]; const index = numbers.findLastIndex(num => num > 10); // Returns 3
சுருக்கம்:
• indexOf() முதல் index-ஐப் பெறுகிறது. • lastIndexOf() கடைசி index-ஐப் பெறுகிறது. • includes() ஒரு உறுப்பு இருக்கிறதா என்று சரிபார்க்கிறது. • find() பொருந்தும் முதல் மதிப்பைத் தருகிறது. • findIndex() பொருந்தும் முதல் index-ஐத் தருகிறது. • findLast() பொருந்தும் கடைசி மதிப்பைத் தருகிறது. • findLastIndex() பொருந்தும் கடைசி index-ஐத் தருகிறது.
ஆதாரம்: https://www.w3schools.com/js/js_array_search.asp
முழுமையான பதிவு: https://dev.to/kamalesh_ar_6252544786997/array-search-methods-in-javascript-23mk
