Thursday, July 4, 2024
HomeLanguagesJavascriptUnderscore.js _.pick() Function

Underscore.js _.pick() Function

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

Syntax:

_.pick(object, *keys)

Parameters: This function accept two parameters as mentioned above and described below:

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

Return Value: It returns a copy of the object that filtered using the given key.

Example 1:




<!DOCTYPE html>
<html>
  
<head>
    <script type="text/javascript" src=
    </script>
</head>
  
<body>
    <script type="text/javascript">
  
        var info = {
            Company: 'neveropen',
            Address: 'Noida',
            Contact: '+91 9876543210'
        };
  
        console.log(_.pick(info, 'Company', 'Address'));
    </script>
</body>
  
</html>


Output:

Example 2:




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

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments