sample
collectionGets a random element from collection.
Signature
sample<T>(collection: T[]): T | undefinedParameters
| Name | Type | Description |
|---|---|---|
collection | T[] | The collection to sample |
Returns
T | undefined - Returns the random element
Examples
Random element
import { sample } from 'dashlite'
const result = sample([1, 2, 3, 4])
console.log(result)Output:
2 (varies)Random winner
import { sample } from 'dashlite'
const participants = ['Alice', 'Bob', 'Charlie']
const winner = sample(participants)
console.log(winner)Output:
'Bob' (varies)Available since version 1.2.0