Skip to content

Function: last()

ts
function last<T, D>(array, defaultValue?): T | D | undefined;

Returns the last item in an array, or a fallback when the array is empty. 返回数组中的最后一个元素;若数组为空则返回兜底值。

Type Parameters

Type ParameterDefault typeDescription
T-The type of array items / 数组元素类型
DundefinedThe fallback value type / 兜底值类型

Parameters

ParameterTypeDescription
arrayreadonly T[]The source array / 源数组
defaultValue?DThe fallback value / 兜底值

Returns

T | D | undefined

The last item or the fallback value / 最后一个元素或兜底值

Example

ts
last([1, 2, 3])
// => 3

Since

1.2.0

Released under the MIT License.