Array.prototype.lastIndexOf()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Array 实例的 lastIndexOf() 方法返回在数组中可以找到给定元素的最后一个索引,如果不存在则返回 -1。从 fromIndex 开始向后搜索数组。

¥The lastIndexOf() method of Array instances returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.

Try it

语法

¥Syntax

js
lastIndexOf(searchElement)
lastIndexOf(searchElement, fromIndex)

参数

¥Parameters

searchElement

要在数组中定位的元素。

fromIndex Optional

开始向后搜索的从零开始的索引,转换为整数

  • 负索引从数组末尾开始倒数 — 如果使用 -array.length <= fromIndex < 0fromIndex + array.length
  • 如果是 fromIndex < -array.length,则不搜索数组并返回 -1。你可以从概念上将其视为从数组开头之前不存在的位置开始,并从那里向后移动。途中没有数组元素,因此永远找不到 searchElement
  • 如果省略 fromIndex >= array.lengthfromIndex,则使用 array.length - 1,导致搜索整个数组。你可以从概念上将其视为从数组末尾之外的不存在位置开始,并从那里向后移动。它最终到达数组的真正结束位置,此时它开始向后搜索实际的数组元素。

返回值

¥Return value

数组中 searchElement 的最后一个索引;如果未找到,则为 -1

¥The last index of searchElement in the array; -1 if not found.

描述

¥Description

lastIndexOf() 方法使用 严格平等(与 === 运算符使用的算法相同)将 searchElement 与数组的元素进行比较。NaN 值永远不会被比较为相等,因此当 searchElementNaN 时,lastIndexOf() 始终返回 -1

¥The lastIndexOf() method compares searchElement to elements of the array using strict equality (the same algorithm used by the === operator). NaN values are never compared as equal, so lastIndexOf() always returns -1 when searchElement is NaN.

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

¥The lastIndexOf() method skips empty slots in sparse arrays.

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

¥The lastIndexOf() method is generic. It only expects the this value to have a length property and integer-keyed properties.

示例

¥Examples

使用 lastIndexOf()

¥Using lastIndexOf()

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

¥The following example uses lastIndexOf() to locate values in an array.

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

¥You cannot use lastIndexOf() to search for NaN.

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

查找某个元素的所有出现位置

¥Finding all the occurrences of an element

以下示例使用 lastIndexOf 查找给定数组中元素的所有索引,并使用 push() 将找到的元素添加到另一个数组中。

¥The following example uses lastIndexOf to find all the indices of an element in a given array, using push() to add them to another array as they are found.

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() 方法不同。

¥Note that we have to handle the case idx === 0 separately here because the element will always be found regardless of the fromIndex parameter if it is the first element of the array. This is different from the indexOf() method.

在稀疏数组上使用 lastIndexOf()

¥Using lastIndexOf() on sparse arrays

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

¥You cannot use lastIndexOf() to search for empty slots in sparse arrays.

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

对非数组对象调用 lastIndexOf()

¥Calling lastIndexOf() on non-array objects

lastIndexOf() 方法读取 thislength 属性,然后访问键为小于 length 的非负整数的每个属性。

¥The lastIndexOf() method reads the length property of this and then accesses each property whose key is a nonnegative integer less than length.

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

规范

Specification
ECMAScript Language Specification
# sec-array.prototype.lastindexof

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看