You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
723 B

4 years ago
  1. # `random()`
  2. The random method returns a random item from the collection:
  3. ```js
  4. const collection = collect([1, 2, 3, 4, 5]);
  5. collection.random();
  6. // 4 (retrieved randomly)
  7. ```
  8. You may optionally pass an integer to random to specify how many items you would like to randomly retrieve. A collection of items is always returned when explicitly passing the number of items you wish to receive:
  9. ```js
  10. const threeRandom = collection.random(3);
  11. // Collection { items: [ 5, 3, 4 ] } (retrieved randomly)
  12. const oneRandom = collection.random(1);
  13. // Collection { items: [ 3 ] } (retrieved randomly)
  14. oneRandom.all();
  15. // [3]
  16. ```
  17. [View source on GitHub](https://github.com/ecrmnn/collect.js/blob/master/src/methods/random.js)