返回指定的值在數組中的最后一個匹配項的索引。
## 語法
~~~
array1.lastIndexOf(searchElement[, fromIndex])
~~~
## 參數
|參數|定義|
|--|--|
|array1|必需。要搜索的數組對象。|
|searchElement|必需。要在 array1 中定位的值。|
|fromIndex|可選。用于開始搜索的數組索引。如果省略 fromIndex,則搜索將從數組中的最后一個索引處開始。|
## 返回值
數組中的 searchElement 的最后一個匹配項的索引;如果未找到 searchElement,則為 -1。
## 備注
lastIndexOf 方法在數組中搜索指定的值。該方法返回第一個匹配項的索引;如果找不到指定的值,則為 -1。
搜索按降序索引順序進行(首先搜索最后一個成員)。若要按升序順序搜索,請使用 indexOf 方法 (Array) (JavaScript)。
數組元素將與 searchElement 值進行全等比較,與使用 === 運算符進行的比較類似。有關更多信息,請參見比較運算符 (JavaScript)。
可選 fromIndex 參數指定用于開始搜索的數組索引。如果 fromIndex 大于或等于數組長度,則搜索整個數組。如果 fromIndex 為負,則搜索從數組長度加上 fromIndex 的位置處開始。如果計算所得的索引小于 0,則返回 -1。
下面的示例闡釋了 lastIndexOf 方法的用法。
~~~
// Create an array.
var ar = ["ab", "cd", "ef", "ab", "cd"];
// Determine the first location, in descending order, of "cd".
document.write(ar.lastIndexOf("cd") + "<br/>");
// Output: 4
// Find "cd" in descending order, starting at index 2.
document.write(ar.lastIndexOf("cd", 2) + "<br/>");
// Output: 1
// Search for "gh" (which is not found).
document.write(ar.lastIndexOf("gh")+ "<br/>");
// Output: -1
// Find "ab" with a fromIndex argument of -3.
// The search in descending order starts at index 3,
// which is the array length minus 2.
document.write(ar.lastIndexOf("ab", -3) + "<br/>");
// Output: 0
~~~
- ActiveXObject對象
- Array對象
- constructor屬性
- length屬性
- prototype屬性
- Array.from函數
- Array.isArray函數
- Array.of函數
- concat方法
- entries方法
- every方法
- fill方法
- filter方法
- findIndex方法
- forEach方法
- indexOf方法
- join方法
- keys方法
- lastIndexOf方法
- map方法
- pop方法
- push方法
- reduce方法
- reduceRight方法
- reverse方法
- shift方法
- slice方法
- some方法
- sort方法
- splice方法
- toString方法
- unshift方法
- valueOf方法
- values方法
- ArrayBuffer對象
- byteLength屬性
- ArrayBuffer.isView函數
- slice方法
- arguments對象
- 0...n 屬性(參數)
- callee 屬性(參數)
- length 屬性 (arguments)
- Boolean對象
- constructor 屬性(布爾值)
- prototype 屬性(布爾值)
- toString 方法 (Boolean)
- valueOf 方法 (Boolean)
- DataView對象
- buffer屬性 (DataView)
- byteLength屬性(DataView)
- byteOffset屬性(DataView)
- getInt8方法(DataView)
- getUint8方法(DataView)
- getInt16方法(DataView)
- getUint16方法(DataView)
- getInt32方法(DataView)
- getUint32方法(DataView)
- getFloat32方法(DataView)
- getFloat64方法(DataView)
- setInt8方法(DataView)
- setUint8方法(DataView)
- setInt16方法(DataView)
- setUint16方法(DataView)
- setInt32方法(DataView)
- setUint32方法(DataView)
- setFloat32方法(DataView)
- setFloat64方法(DataView)
- Date對象
- Debug對象
- Enumerator對象
- Error對象
- Float32Array對象
- Float64Array對象
- Function對象
- Global對象
- Int8Array對象
- Int16Array對象
- Int32Array對象
- Intl.Collator對象
- Intl.DateTimeFormat對象
- Intl.NumberFormat對象
- JSON對象
- Map對象
- Math對象
- Number對象
- Object對象
- Promise對象
- 代理對象
- Reflect對象
- RegExp對象
- 正則表達式對象
- Set對象
- String對象
- 符號對象
- Uint8Array對象
- Uint8ClampedArray對象
- Uint16Array對象
- Uint32Array對象
- VBArray對象
- WeakMap對象
- WeakSet對象
- WinRTError對象