Open In App

Lodash_.shuffle() Method

Last Updated : 03 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Lodash _.shuffle() method shuffles of collection by returning the new array.

Syntax: 

_.shuffle(collection);

Parameters:

  • collection: This parameter holds the collection to inspect.

Return Value:

This method returns the new array after shuffling.

Example 1: In this example, we are getting the shuffled value of the given array in the console.

Javascript




// Requiring the lodash library
const _ = require("lodash");
 
// Original array and use _.shuffle() method
let gfg = _.shuffle([1, 2, 3, 4]);
 
// Printing the output
console.log(gfg);


Output :

[4, 1, 3, 2]

Example 2: In this example, we are getting the shuffled value of the given array in the console.

Javascript




// Requiring the lodash library
const _ = require("lodash");
 
// Original array and use _.shuffle() method
let gfg = _.shuffle(["g", "e", "e", "k", "s", "f", "o",
    "r", "g", "e", "e", "k", "s"]);
 
// Printing the output
console.log(gfg);


Output :

["o", "e", "k", "s", "e", "e", "s",
 "g", "r", "e", "g", "f", "k"]

Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads