Open In App

Underscore.js _.pick() Function

Last Updated : 14 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Underscore.js _.pick() function is used to return a copy of the object that is filtered using the given key. This function accepts a predicate indicating which keys are picked from the object.

Syntax:

_.pick(object, *keys);

Parameters:

  • object: This parameter holds the value of an object.
  • keys: It is an optional parameter. It contains the key name that the value needs to be picked.

Return Value:

It returns a copy of the object that is filtered using the given key.

Example 1: This example shows the use of the Underscore.js _.pick() Function.

html
<!DOCTYPE html>
<html>

<head>
    <script type="text/javascript" src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
    </script>
</head>

<body>
    <script type="text/javascript">

        let info = {
            Company: 'GeeksforGeeks',
            Address: 'Noida',
            Contact: '+91 9876543210'
        };

        console.log(_.pick(info, 'Company', 'Address')); 
    </script>
</body>

</html>

Output:

Example 2: This example shows the use of the Underscore.js _.pick() Function.

html
<!DOCTYPE html>
<html>

<head>
    <script type="text/javascript" src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
    </script>
</head>

<body>
    <script type="text/javascript">

        let info = {
            key1: 10,
            key2: 20,
            key3: 30,
            key4: 40,
            key5: 50
        };

        console.log(_.pick(info, function (value, key, info) {
            return value == 10 || value == 50;
        })); 
    </script>
</body>

</html>

Output:


Next Article

Similar Reads