9951 explained code solutions for 126 technologies


javascript-lodashHow do I use Lodash to shuffle an array in JavaScript?


Using Lodash to shuffle an array in JavaScript is a simple process. First, import the library:

const _ = require('lodash');

Then, create an array:

const arr = [1, 2, 3, 4, 5];

Finally, use the shuffle method to shuffle the array:

const shuffledArr = _.shuffle(arr);
// [4, 5, 1, 3, 2]

The shuffle method takes the array and returns a shuffled version of it.

  • require('lodash'): imports the Lodash library
  • _.shuffle(arr): takes the array arr and returns a shuffled version of it

Helpful links

Edit this code on GitHub