`_.shuffle` function on array using JavaScript Library underscore.js

1 year ago
2

Hi Friends,

`_.shuffle` function on array using JavaScript Library underscore.js

`_.shuffle` is a utility function in Underscore.js library that allows you to randomly shuffle the elements of an array. It provides a simple way to randomize the order of elements in a collection.

In below example, we have an array of numbers numbers. We use _.shuffle to create a new array shuffledNumbers with the same elements as numbers but in a random order. The output will vary each time you run the code due to the random nature of the shuffling.

Note: _.shuffle does not modify the original array; it returns a new shuffled array. If you want to shuffle the elements in place, you can assign the result back to the original array.

// syntax
_.shuffle(array)

// example
var numbers = [1, 2, 3, 4, 5];

var shuffledNumbers = _.shuffle(numbers);

console.log(shuffledNumbers);
// Example output: [3, 1, 5, 4, 2]

#javascript #under #score #underscore #underscorejs #shuffle #underscoreshuffle #shufflefunction #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official

Loading comments...