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 Parameter | Default type | Description |
|---|---|---|
T | - | The type of array items / 数组元素类型 |
D | undefined | The fallback value type / 兜底值类型 |
Parameters
| Parameter | Type | Description |
|---|---|---|
array | readonly T[] | The source array / 源数组 |
defaultValue? | D | The fallback value / 兜底值 |
Returns
T | D | undefined
The last item or the fallback value / 最后一个元素或兜底值
Example
ts
last([1, 2, 3])
// => 3Since
1.2.0