Open In App

Underscore.js _.sample() Function

Last Updated : 17 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 list or the array which will be tested in this function.
  • n: It is the number of elements that will be returned by the function.

Return values:

It returns the specified number of random elements from the passed array or only one random element by default.

Passing a list of numbers to the _.sample() function

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.

Example: The below code example implements the _.sample() function with a array of numbers without passing second parameter.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        console.log(_.sample([1, 2, 3, 4, 5, 6]));
    </script>
</body>
 
</html>


Output:

Passing the second parameter to the _.sample() function:

If 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.

Example: The below code will explain the use of the _.sample() function by passing the second argument.

html




<!DOCTYPE html>
<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:

Output

Passing a structure to the _.sample() function:

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.

Example: The below code will illustrate the use of the _.sample() function by passing structure as an argument.

html




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        const 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:

Passing a structure with only one property to the _.sample() function together:

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.

Example: The below code will use the _.sample() function with the structure with only one property.

html




<!DOCTYPE html>
<html>
 
<head>
    <script src =
    </script>
</head>
 
<body>
    <script type="text/javascript">
        const users = [
                {"num":"10"},
                {"num":"9"},
                {"num":"8"},
                {"num":"7"},
                {"num":"6"}
            ];
    console.log(_.sample(users));
    </script>
</body>
 
</html>


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads