Open In App

Underscore.js _.findKey() Function

Last Updated : 25 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report
The _.findKey() function is used to return the key value where the predicate logic or condition returns the truth value or undefined. The predicate function/condition is transformed through iteratee to facilitate shorthand syntaxes. Syntax:
_.findKey(object, predicate, [context])
Parameters: This function accept three parameters as mentioned above and described below:
  • list: This parameter is used to hold the list of items.
  • predicate: This parameter is used to hold the truth condition.
  • context: The text content which need to be display. It is optional parameter.
Return Value: It returns the key value where the predicate logic or condition returns truth or undefined. Example 1: 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">

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

        var key = _.findKey(info, function (value) {
            return value === 'GeeksforGeeks';
        });
        
        console.log(key);
    </script>
</body>

</html>
Output: Example 2: 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">

        var info = {
            key1: 'Geeks',
            key2: 'GFG',
            key3: 'GeeksforGeeks',
            key4: 'Hello',
            key5: 'Welcome'
        };

        var key = _.findKey(info, function (value) {
            return value.length == 13;
        });

        console.log(key);
    </script>
</body>

</html>
Output:

Next Article

Similar Reads