Open In App

Underscore.js _.findKey() Function

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:

Return Value: It returns the key value where the predicate logic or condition returns truth or undefined.



Example 1:




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




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


Article Tags :