@pmun/utils / sample
Function: sample()
ts
function sample<T>(array): undefined | T;
从数组中随机选择一个元素
Type Parameters
T
T
Parameters
array
T
[]
数组
Returns
undefined
| T
随机选择的元素,如果数组为空则返回 undefined
Example
ts
const fruits = ['苹果', '香蕉', '橙子', '梨', '葡萄']
const randomFruit = sample(fruits)
// 可能返回: '香蕉'
const empty = []
const emptyResult = sample(empty) // undefined