Underscore.js _.shuffle Function
Last Updated :
23 Dec, 2022
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=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</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=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</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=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</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=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</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"
}]
Similar Reads
Underscore.js _.sample() Function Underscore.js _.sample() function is used to find out what kind of elements are present in the array. It gives a random element of the array as output. We can even pass a second parameter to return that number of random elements from the array. Syntax:_.sample(list, [n])Parameters:list: It is the li
3 min read
Underscore.js _.reduce() Function Underscore.js _.reduce() function is an inbuilt function in Underscore.js that is used to transform an array's/object's properties into one single value or is used to create a single result from a given list of values. When all the elements of the list are passed to the function/iterate and no more
3 min read
Underscore.js _.map() Function The Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects. The _.map() function is an inbuilt function in Underscore.js library of the JavaScript which is used
4 min read
Underscore.js _.reject Function The Underscore.js is a JavaScript library that provides a lot of useful functions that helps in the programming in a big way like the map, filter, invoke etc even without using any built-in objects. The _.reject() function is used to give the answer which does not match with the given condition. It
3 min read
Underscore.js _.sortBy Function Underscore.js _.sortBy() function is used to sort all the elements of the list in ascending order according to the function given to it as a parameter. Passing the array with a function that returns the number and will sort the array in ascending order and return an array. The array can be both nume
3 min read
Underscore.js _.memoize() Function The _.memoize() function is used to memorize a given function by caching the result computed by the function. It is used to speed up for the slow running process. Syntax: _.memoize(function, [hashFunction]) Parameters: This function accepts two parameters as mentioned above and described below: func
1 min read
Underscore.js _.mixin() Function Underscore.js is a JavaScript library that makes operations on arrays, string, objects much easier and handy. The _.mixin() function is used to add extra functionality and extend the global underscore object to some special utility functions. Its important to link the underscore CDN before going and
2 min read
Underscore.js _.zip() Function The Underscore.js is a JavaScript library that provides a lot of useful functions like the map, filter, invoke etc even without using any built-in objects. The _.zip() function matches each passed array of elements to the next passed array element. It is used when more than one homogeneous arrays (o
3 min read
Underscore.js _.noConflict() Function Underscore.js is a library in javascript that makes operations on arrays, string, objects much easier and handy. The _.noConflict() function is used to create a reference of the global underscore object "_" to another variable. Note: It is very necessary to link the underscore CDN before going and u
2 min read