_.sampleSize(collection, [n=1])

sourcenpm package

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

Since

4.0.0

Arguments

  • collection (Array|Object): The collection to sample.
  • [n=1] (number): The number of elements to sample.

Returns

(Array): Returns the random elements.

Example

  1. _.sampleSize([1, 2, 3], 2);
    // => [3, 1]
    _.sampleSize([1, 2, 3], 4);
    // => [2, 3, 1]