sampleSize

collection

Gets n random elements at unique keys from collection up to the size of collection.

Signature

sampleSize<T>(collection: T[], n: number): T[]

Parameters

NameTypeDescription
collectionT[]The collection to sample
nnumberThe number of elements to sampleDefault: 1

Returns

T[] - Returns the random elements

Examples

Sample multiple

import { sampleSize } from 'dashlite'

const result = sampleSize([1, 2, 3, 4, 5], 3)
console.log(result)

Output:

[3, 1, 5] (order varies)

Sample winners

import { sampleSize } from 'dashlite'

const participants = ['A', 'B', 'C', 'D', 'E']
const winners = sampleSize(participants, 2)
console.log(winners)

Output:

['D', 'B'] (varies)

Available since version 1.2.0