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 _.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 so as to return that number of random elements from the array.
Syntax:
_.sample(list, [n])
Parameters:It takes two arguments:
- The List
- The number n
Return values:
It returns an of the element from the passed array.
- The ._sample() function uses a random function and then displays that element from the list as the result. If the second parameter is not mentioned then t’s default value will be taken which is 1. Hence, any one of the element will be displayed.
<!-- Write HTML code here --> < html > < head > < script src = </ script > </ head > < body > < script type = "text/javascript" > console.log(_.sample([1, 2, 3, 4, 5, 6])); </ script > </ body > </ html > |
Output:
Id we pass the second parameter then the _.sample() function will return as many elements from the passed list as mentioned. The result will be an array containing the number of elements which is present in the second parameter.
<!-- Write HTML code here --> < html > < head > < script src = </ script > </ head > < body > < script type = "text/javascript" > console.log(_.sample([1, 2, 3, 4, 5, 6], 3)); </ script > </ body > </ html > |
Output:
We can even pass a structure to the _.sample() function and it will work in the same manner. It will display any of the element of the structure randomly as the output. Since, no second parameter is mentioned therefore, it will have only one element of the passed list in the result along with it’s all properties.
<!-- Write HTML code here --> < html > < head > < script src = </ script > </ head > < body > < script type = "text/javascript" > var people = [ {"name": "sakshi", "hasLong": "false"}, {"name": "aishwarya", "hasLong": "true"}, {"name": "akansha", "hasLong": "true"}, {"name": "preeti", "hasLong": "true"} ] console.log(_.sample(people)); </ script > </ body > </ html > |
Output:
If we pass a structure with only one property then it will work in the same way and display any one of the element randomly from the structure passed. Here also, since the second parameter is not mentioned therefore, the resultant array will contain only one element.
<!-- Write HTML code here --> < html > < head > < script src = </ script > </ head > < body > < script type = "text/javascript" > var users = [{"num":"10"}, {"num":"9"}, {"num":"8"}, {"num":"7"}, {"num":"6"}]; console.log(_.sample(users)); </ script > </ body > </ html > |
Output:
NOTE:
These commands will not work in Google console or in firefox as for these additional files need to be added which they didn’t have added.
So, add the given links to your HTML file and then run them.
The links are as follows:
<!-- Write HTML code here --> < script type = "text/javascript" src = </ script > |
An example is shown below: