Open In App

Underscore.js _.shuffle Function

Last Updated : 23 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The Underscore.js _.shuffle() function is used to arrange a list of arrays in a random manner. This _.shuffle() underscore function uses Fisher Yates Shuffle which is discussed in the below-mentioned article. So, every time we use this the output of this function will be different according to the Fisher Yates Shuffle. 

Syntax:

_.shuffle(list)

Parameters: This function accepts a single parameter List. This parameter is used to hold the list of items that will be shuffled. 

Return values: The returned value is the new randomized array containing all the elements which are in the original array which is passed to the _.shuffle() function. 

Passing a numeric array to the _.shuffle() function: The ._shuffle() function takes the element from the list one by one and does the specified operations according to the fisher Yates Shuffle. Then console.log() is the final answer which will contain all the elements of the original array in the randomized problem. 

Example: 

html




<script src=
</script>
  
<script type="text/javascript">
    console.log(_.shuffle(_.shuffle([1, 2, 3, 4, 5, 6])));
</script>


Output: The output shuffles every time we run the code.

[1, 2, 3, 6, 5, 4] //1st time
[3, 6, 2, 4, 5, 1] //2nd time
[5, 6, 1, 4, 3, 2] //3rd time

Passing a structure to the _.shuffle() function: Passing a structure to the _.shuffle() function. First, declare the array like here the array is ‘goal’ and then pass this array to the _.shuffle() function. The elements of the ‘goal’ array will be shuffled along with all their properties. 

Example: This example demonstrates the above-used approach.

html




<script src=
</script>
  
<script type="text/javascript">
    var goal = [
    {
        "category" : "other",
        "title" : "harry University",
        "value" : 50000,
        "id":"1"
    },
    {
        "category" : "travelling",
        "title" : "tommy University",
        "value" : 50000,
        "id":"2"
    },
    {
        "category" : "education",
        "title" : "jerry University",
        "value" : 50000,
        "id":"3"
    },
    {
        "category" : "business",
        "title" : "Charlie University",
        "value" : 50000,
        "id":"4"
    }
            ]
            console.log(_.shuffle(goal));
</script>


Output: The output shuffles every time we run the code.

[[object Object] {
  category: "business",
  id: "4",
  title: "Charlie University",
  value: 50000
}, [object Object] {
  category: "travelling",
  id: "2",
  title: "tommy University",
  value: 50000
}, [object Object] {
  category: "education",
  id: "3",
  title: "jerry University",
  value: 50000
}, [object Object] {
  category: "other",
  id: "1",
  title: "harry University",
  value: 50000
}]

Passing a list with one property as true/false to the _.shuffle() function: First, declare the array (here array is ‘people’). Choose one condition on which need to check like here ‘hasLongHairs’. Console.log the final answer. The final answer will be a randomized array as the fisher yates shuffle uses a random function in it’s algorithm. 

Example: This example demonstrates the above-used approach.

html




<script src=
</script>
  
<script type="text/javascript">
    var people = [
    {"name": "sakshi", "hasLong": "false"},
    {"name": "aishwarya", "hasLong": "true"},
    {"name": "akansha", "hasLong": "true"},
    {"name": "preeti", "hasLong": "true"}
            ]
            console.log(_.shuffle(people, 'name'));
</script>


Output: The output shuffles every time we run the code.

[[object Object] {
  hasLong: "true",
  name: "preeti"
}, [object Object] {
  hasLong: "true",
  name: "akansha"
}, [object Object] {
  hasLong: "false",
  name: "sakshi"
}, [object Object] {
  hasLong: "true",
  name: "aishwarya"
}]

Declaring an array and then passing it to the _.shuffle() function: Declaring an array let ‘users’ with a property as ‘num’ and then pass it to the _.shuffle() function. Then console.log the new randomized array. The output will be different every time you run it.

Example: This example demonstrates the above-used approach.

html




<script src=
</script>
  
<script type="text/javascript">
    var users = [{"num":"1"}, {"num":"2"}, {"num":"3"}, 
                 {"num":"4"}, {"num":"5"}];
            console.log(_.shuffle(users, 'id'));
</script>


Output: The output shuffles every time we run the code.

[[object Object] {
  num: "5"
}, [object Object] {
  num: "4"
}, [object Object] {
  num: "3"
}, [object Object] {
  num: "1"
}, [object Object] {
  num: "2"
}]


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

Similar Reads