@pengzhanbo/utils - v3.4.1
    Preparing search index...

    Function sample

    • Gets a random element from an array.

      从数组中获取一个随机元素。

      Type Parameters

      • T

      Parameters

      • array: readonly T[]

        The array to sample from. 要取样的数组

      Returns T | undefined

      A random element from the array, or undefined if the array is empty. 数组中的随机元素,如果数组为空则返回 undefined

      O(1) time complexity - direct index access

      O(1) 时间复杂度 - 直接索引访问

      sample([1, 2, 3, 4, 5])
      // => 2 (random element from the array)
      sample([])
      // => undefined