Open In App
Related Articles

Underscore.js _.pick() Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
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=
    </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=
    </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:



Last Updated : 14 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads