WebGL Tutorial
and more

findLast

撰写时间:2024-03-15

最新修订:2024-03-15

findLast

返回数组中满足回调函数所设定的条件的最后一个元素。

原型

* | UndefinedfindLast
  • functioncallbackFn
  • *[]thisArg

参数

callbackFn

回调函数。该回调函数接受3个参数。其原型如下:

booleancallbackFn
  • *element
  • numberindex
  • *[]array
element
当前遍历到的数组元素。
index
当前遍历到的数组索引值。
array
调用findLast方法的数组实例。

回调函数callbackFn返回boolean类型的数值。

thisArg
调用findLast方法的数组实例。可选。

返回值

如果有满足回调函数所设定条件的元素,则返回最后一个元素。

如果所有元素均未满足回调函数所设定的条件,则返回undefined

说明

findLast方法通过调用回调函数来遍历数组元素,并在回调函数中设定一个适用于数组全体元素的条件。

如果有至少一个元素满足回调函数所设定的条件,则返回满足条件的最后一个元素。如果所有元素均未满足回调函数所设定的条件,则返回undefined

例子

基本用法

import {NumUtils} from '/js/esm/NumUtils.js'; let nums = NumUtils.GetIntsInRange(-100, 100, 5); /* [22, -9, -72, 8, -13] */ let result = nums.findLast(num => num < 0); /* -13 */

没有满足条件时返回undefined

let nums = [1, 2, 3, 4, 5]; let result = nums.findLast(num => num < 0); /* undefined */

参见

  1. find
  2. filter

参考资源

  1. ECMA 262: Array Objects