Function: first()
ts
function first<T, D>(array, defaultValue?): T | D | undefined;Returns the first 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 first item or the fallback value / 第一个元素或兜底值
Example
ts
first([1, 2, 3])
// => 1Since
1.2.0