Lodash _.shuffle() Method
Lodash _.shuffle() method creates an array of shuffled values from the given collection using a version of the Fisher-Yates shuffle algorithm.
Syntax:
_.shuffle(collection);
Parameters:
- collection: This parameter holds the collection to shuffle.
Return Value:
This method is used to return the new shuffled array.
Example 1: In this example, we are shuffling an array by the use of the _.shuffle() method.
javascript
// Requiring the lodash library const _ = require( "lodash" ); // Original array let array = [2, 4, 6, 9, 10]; // Use of _.shuffle() method let shuffled_array = _.shuffle(array); // Printing the output console.log(shuffled_array); |
Output:
[ 10, 9, 4, 2, 6 ]
Example 2: In this example, we are shuffling an array by the use of the _.shuffle() method.
javascript
// Requiring the lodash library const _ = require( "lodash" ); // Original array let array = [ 'mon' , 'tue' , 'wed' , 'thu' , 'fri' , 'sat' , 'sun' ]; // Use of _.shuffle() method let shuffled_array = _.shuffle(array); // Printing the output console.log(shuffled_array); |
Output:
[ 'sat', 'wed', 'fri', 'sun', 'thu', 'mon', 'tue']
Contact Us