Skip to content

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 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 first item or the fallback value / 第一个元素或兜底值

Example

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

Since

1.2.0

Released under the MIT License.