@nevware21/ts-utils
    Preparing search index...

    Function arrSample

    • The arrSample() method returns a random element from the array. Returns undefined if the array is empty or null/undefined.

      Type Parameters

      • T

        Identifies the base type of array elements

      Parameters

      • theArray: ArrayLike<T>

        The array or array-like object to sample from

      Returns T

      A random element from the array, or undefined if empty

      0.14.0

      arrSample([1, 2, 3, 4, 5]);  // e.g., 3
      arrSample(["a", "b", "c"]); // e.g., "b"
      arrSample([42]); // 42
      arrSample([]); // undefined
      arrSample(null); // undefined

      // Array-like objects
      arrSample({ length: 3, 0: "x", 1: "y", 2: "z" }); // e.g., "y"