Pole JavaScript lastIndexOf ()

Metoda JavaScript Array lastIndexOf () vrací poslední index, u kterého lze daný prvek v poli najít, nebo -1, pokud není přítomen.

Syntaxe lastIndexOf()metody je:

 arr.lastIndexOf(searchElement, fromIndex)

Zde je arr pole.

parametry lastIndexOf ()

lastIndexOf()Metoda bere v:

  • searchElement - prvek, který se má vyhledat v poli.
  • fromIndex (volitelné) - Index, který má začít hledat zpět. Ve výchozím nastavení je to array.length - 1 .

Vrátit hodnotu z lastIndexOf ()

  • Vrátí poslední index prvku v poli, pokud je přítomen alespoň jednou.
  • Vrátí -1, pokud prvek není v poli nalezen.

Poznámka: ve lastIndexOf() srovnání searchElements prvky pole pomocí přísné rovnosti (podobně jako operátor triple-equals nebo ===).

Příklad 1: Použití metody 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

Výstup

 3 4 0 -1 

Poznámky:

  • Pokud fromIndex <0 , index se počítá zpět. Například -1 označuje poslední prvek atd.
  • Pokud je vypočítaný index, tj. Array.length + fromIndex <0 , vrátí se -1 .

Příklad 2: Nalezení všech výskytů prvku

 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); // ()

Výstup

 (7, 4, 0) (1) ()

Zde je if (currentIndex> 0)příkaz přidán tak, aby výskyty v indexu 0 nedaly -1 pro currentIndex - 1. To by vedlo k opětovnému vyhledávání zezadu a program by byl zachycen v nekonečné smyčce.

Příklad 3: Zjištění, zda prvek existuje jinak Přidání prvku

 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.

Výstup

Prvek nebyl nalezen! Aktualizováno pole. ('Monitor', 'Keyboard', 'Mouse', 'Speaker', 'CPU') Myš je již v poli.

Doporučené četby:

  • Pole JavaScript
  • JavaScript Array.indexOf ()

Zajímavé články...