Lodash - sample method



Syntax

_.sample(collection)

Gets a random element from collection.

Arguments

  • collection (Array|Object) − The collection to sample.

Output

  • (*) − Returns the random element.

Example

var _ = require('lodash');
var list = [1, 2, 3, 4, 5]
var result = _.sample(list);
console.log(result);

Save the above program in tester.js. Run the following command to execute this program.

Command

\>node tester.js

Output

3
lodash_collection.htm
Advertisements