lastIndexOf (Lodash兼容性)
使用Array.lastIndexOf
此lastIndexOf函数由于处理null或undefined、搜索NaN值等原因运行缓慢。
请使用更快、更现代的Array.lastIndexOf。
查找数组中指定元素最后出现的索引。
typescript
const index = lastIndexOf(array, searchElement, fromIndex);参考
lastIndexOf(array, searchElement, fromIndex)
返回数组中指定元素最后出现的索引。与原生Array.lastIndexOf类似,但也可以找到NaN值。
typescript
import { lastIndexOf } from 'es-toolkit/compat';
// 基本用法
lastIndexOf([1, 2, 1, 2], 2);
// => 3
// 指定起始索引
lastIndexOf([1, 2, 1, 2], 2, 2);
// => 1
// 查找NaN值(原生lastIndexOf无法找到NaN)
lastIndexOf([1, 2, NaN, 4, NaN], NaN);
// => 4
// 使用负索引
lastIndexOf([1, 2, 3, 4], 3, -2);
// => 2null或undefined被视为空数组。
typescript
import { lastIndexOf } from 'es-toolkit/compat';
lastIndexOf(null, 1); // -1
lastIndexOf(undefined, 1); // -1参数
array(ArrayLike<T> | null | undefined): 要搜索的数组。searchElement(T): 要查找的元素。fromIndex(true | number, 可选): 开始搜索的索引。传递true从数组末尾开始搜索。默认值为array.length - 1。
返回值
(number): 返回最后一个匹配元素的索引。如果找不到则返回-1。

