返回指定的值在数组中的最后一个匹配项的索引。
## 语法
~~~
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对象