जावास्क्रिप्ट अर्रे lastIndexOf ()

जावास्क्रिप्ट ऐरे lastIndexOf () विधि अंतिम इंडेक्स देता है जिस पर किसी दिए गए तत्व को सरणी में पाया जा सकता है, या -1 मौजूद नहीं होने पर।

lastIndexOf()विधि का सिंटैक्स है:

 arr.lastIndexOf(searchElement, fromIndex)

यहाँ, अरै एक अरै है।

lastIndexOf () पैरामीटर

lastIndexOf()विधि में लेता है:

  • searchElement - सरणी में पता लगाने वाला तत्व।
  • fromIndex (वैकल्पिक) - पीछे की ओर खोज शुरू करने के लिए सूचकांक। डिफ़ॉल्ट रूप से यह array.length - 1 है

LastIndexOf से वापसी मान ()

  • यदि यह कम से कम एक बार मौजूद है, तो तत्व के अंतिम सूचकांक को वापस लौटाता है।
  • तत्व में सरणी नहीं मिला है, तो रिटर्न -1

नोट: एरे के तत्वों की lastIndexOf()तुलना सख्त समानता (ट्रिपल-बराबर ऑपरेटर या के समान) searchElementका उपयोग करके की जाती है ।===

उदाहरण 1: lastIndexOf () विधि का उपयोग करना

 var priceList = (10, 8, 2, 31, 10, 1, 65); // lastIndexOf() returns the last occurance var index1 = priceList.lastIndexOf(31); console.log(index1); // 3 var index2 = priceList.lastIndexOf(10); console.log(index2); // 4 // second argument specifies the backward search's start index var index3 = priceList.lastIndexOf(10, 3); console.log(index3); // 0 // lastIndexOf returns -1 if not found var index4 = priceList.lastIndexOf(69.5); console.log(index4); // -1

आउटपुट

 3 4 0 -1 

टिप्पणियाँ:

  • यदि इंडेक्सएक्स <0 से , तो सूचकांक पीछे की गणना की जाती है। उदाहरण के लिए, -1 अंतिम तत्व वगैरह को दर्शाता है।
  • यदि गणना किए गए सूचकांक यानी array.length + fromIndex <0 , -1 को लौटा दिया गया है।

उदाहरण 2: एक तत्व के सभी अवसरों का पता लगाना

 function findAllIndex(array, element) ( indices = (); var currentIndex = array.lastIndexOf(element); while (currentIndex != -1) ( indices.push(currentIndex); if (currentIndex> 0) ( currentIndex = array.lastIndexOf(element, currentIndex - 1); ) else ( currentIndex = -1; ) ) return indices; ) var priceList = (10, 8, 2, 31, 10, 1, 65, 10); var occurance1 = findAllIndex(priceList, 10); console.log(occurance1); // ( 7, 4, 0 ) var occurance2 = findAllIndex(priceList, 8); console.log(occurance2); // ( 1 ) var occurance3 = findAllIndex(priceList, 9); console.log(occurance3); // ()

आउटपुट

 (0, ४, ०) (१) ()

इधर, if (currentIndex> 0)बयान इसलिए जोड़ा जाता है कि सूचकांक में घटनाओं 0 नहीं देंगे -1 के लिए currentIndex - 1। इससे पीछे से फिर से खोज की जा सकती है और कार्यक्रम एक अनंत लूप में फंस जाएगा।

उदाहरण 3: ढूँढना यदि तत्व मौजूद है तो तत्व जोड़ना

 function checkOrAdd(array, element) ( if (array.lastIndexOf(element) === -1) ( array.push(element); console.log("Element not Found! Updated the array."); ) else ( console.log(element + " is already in the array."); ) ) var parts = ("Monitor", "Keyboard", "Mouse", "Speaker"); checkOrAdd(parts, "CPU"); // Element not Found! Updated the array. console.log(parts); // ( 'Monitor', 'Keyboard', 'Mouse', 'Speaker', 'CPU' ) checkOrAdd(parts, "Mouse"); // Mouse is already in the array.

आउटपुट

तत्व नहीं मिला! सरणी अद्यतन किया गया। ('मॉनिटर', 'कीबोर्ड', 'माउस', 'स्पीकर', 'सीपीयू') माउस पहले से ही ऐरे में है।

अनुशंसित रीडिंग:

  • जावास्क्रिप्ट ऐरे
  • जावास्क्रिप्ट Array.indexOf ()

दिलचस्प लेख...