Skip to content
Related Articles

Related Articles

Improve Article

Lodash _.shuffle() Method

  • Last Updated : 06 Sep, 2020

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, collection, strings, objects, numbers etc.

The _.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: This method accepts single parameter as mentioned above and described below:

  • collection: This parameter holds the collection to shuffle.

Return Value: This method is used to return the new shuffled array.



Example 1:




// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array 
var 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:




// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array 
var 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']
My Personal Notes arrow_drop_up
Recommended Articles
Page :