Skip to content
Related Articles

Related Articles

Improve Article

Lodash _.shuffle() Method

  • Last Updated : 06 Sep, 2020
Geek Week

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']

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!

My Personal Notes arrow_drop_up
Recommended Articles
Page :