Open In App

Collect.js | random() function

Last Updated : 05 Jun, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

The random() function as the name suggest it returns any random value from the collection. In JavaScript, the array is first converted to a collection and then the function is applied to the collection.

Syntax: 

data.random(number)

Parameters: This function accept a single parameter as mentioned above and described below

  • number: This parameter holds the integer number that how many random number should return from the collection.

Return Value: Returns random value or values from collection.

Below examples illustrate the random() function in collect.js

Example 1:Return single random value.
 

Javascript




// It is used to import collect.js library
const collect = require('collect.js');
  
let main = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const data = collect(main);
let x = data.random();
console.log(x);


Output: 

6

Example 2: Return multiple random values.

Javascript




// It is used to import collect.js library
const collect = require('collect.js');
  
let main = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const data = collect(main);
  
let x2 = data.random(2);
console.log(x2.all());


Output: 

[5 , 7]

Reference: https://collect.js.org/api/random.html



Next Article

Similar Reads