pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf

href="?u=http://developer.mozilla.org/static/client/inter-latin.9a3b1bc220d426ef.woff2" as="font" type="font/woff2" crossorigen="anonymous" fetchpriority="low" />

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Array.prototype.lastIndexOf()

基线 广泛可用

自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

lastIndexOf() 方法返回数组中给定元素最后一次出现的索引,如果不存在则返回 -1。该方法从 fromIndex 开始向前搜索数组。

尝试一下

const animals = ["Dodo", "Tiger", "Penguin", "Dodo"];

console.log(animals.lastIndexOf("Dodo"));
// Expected output: 3

console.log(animals.lastIndexOf("Tiger"));
// Expected output: 1

语法

js
lastIndexOf(searchElement)
lastIndexOf(searchElement, fromIndex)

参数

searchElement

被查找的元素。

fromIndex 可选

以 0 起始的索引,表明反向搜索的起始位置,会被转换为整数

  • 如果 fromIndex < 0,则从数组末尾开始倒数计数——即使用 fromIndex + array.length 的值。
  • 如果 fromIndex < -array.length,则不搜索数组并返回 -1。从概念上讲,你可以把它想象成从数组开始之前不存在的位置开始反向搜索,这条路径上没有任何数组元素,因此 searchElement 永远不会被找到。
  • 如果 fromIndex >= array.length 或者省略了 fromIndex,则使用 array.length - 1,这会导致搜索整个数组。可以将其理解为从数组尾部之后不存在的位置开始向前搜索。最终会访问到数组最后一个元素,并继续向前开始实际搜索数组元素。

返回值

数组中该元素最后一次出现的索引,如未找到返回 -1

描述

lastIndexOf 使用严格相等(与 === 运算符使用的算法相同)比较 searchElement 和数组中的元素。NaN 值永远不会被比较为相等,因此当 searchElementNaNlastIndexOf() 总是返回 -1

lastIndexOf() 方法会跳过稀疏数组中的空槽。

lastIndexOf() 方法是通用的。它只期望 this 值具有 length 属性和整数键属性。

示例

使用 lastIndexOf()

下例使用 lastIndexOf() 定位数组中的值。

js
const numbers = [2, 5, 9, 2];
numbers.lastIndexOf(2); // 3
numbers.lastIndexOf(7); // -1
numbers.lastIndexOf(2, 3); // 3
numbers.lastIndexOf(2, 2); // 0
numbers.lastIndexOf(2, -2); // 0
numbers.lastIndexOf(2, -1); // 3

你不能用 lastIndexOf() 来搜索 NaN

js
const array = [NaN];
array.lastIndexOf(NaN); // -1

查找元素出现的所有索引

下例使用 lastIndexOf 查找到一个元素在数组中所有的索引(下标),并在找到它们时用 push 将它们添加到另一个数组中。

js
const indices = [];
const array = ["a", "b", "a", "c", "a", "d"];
const element = "a";
let idx = array.lastIndexOf(element);
while (idx !== -1) {
  indices.push(idx);
  idx = idx > 0 ? array.lastIndexOf(element, idx - 1) : -1;
}

console.log(indices);
// [4, 2, 0]

需要注意的是,这里必须单独处理 idx === 0 的情况,因为如果该元素是数组的第一个元素,则无论 fromIndex 参数的值为何,它总是会被找到。这与 indexOf 方法不同。

在稀疏数组上使用 lastIndexOf()

你不能使用 lastIndexOf() 来搜索稀疏数组中的空槽。

js
console.log([1, , 3].lastIndexOf(undefined)); // -1

在非数组对象上调用 lastIndexOf()

lastIndexOf() 方法读取 thislength 属性,然后访问每个整数索引。

js
const arrayLike = {
  length: 3,
  0: 2,
  1: 3,
  2: 2,
};
console.log(Array.prototype.lastIndexOf.call(arrayLike, 2));
// 2
console.log(Array.prototype.lastIndexOf.call(arrayLike, 5));
// -1

规范

规范
ECMAScript® 2027 Language Specification
# sec-array.prototype.lastindexof

浏览器兼容性

参见

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy