ixfx
    Preparing search index...

    Function randomPluck

    Plucks a random value from an array, optionally mutating the original array.

    const { value, remainder } = randomPluck(inputArray);
    
    const value = randomPluck(inputArray, { mutate: true });
    

    If the input array is empty, undefined is returned as the value.

    Type of items in array

    Array to pluck item from

    Options. By default { mutate: false, source: Math.random }

    Random generator. Math.random by default.

    • Returns a random value from array, and removes it from the array.

      const data = [100,20,50];
      const v = randomPluck(data, { mutate: true });
      // eg: v: 20, data is now [100,50]

      Type Parameters

      • V

      Parameters

      • array: readonly V[] | V[]
      • options: { mutate: true; source?: RandomSource }

      Returns undefined | V

    • Returns a random element from an array along with the remaining elements. Does not modify the original array.

      const data = [100,20,50];
      const {value,remainder} = randomPluck(data);
      // eg: value: 20, remainder: [100,50], data remains [100,20,50]

      Type Parameters

      • V

      Parameters

      • array: readonly V[] | V[]
      • Optionaloptions: { mutate: false; source?: RandomSource }

      Returns { remainder: V[]; value: V }