Skip to content
Related Articles

Related Articles

Improve Article

Underscore.js | _.findKey() Function

  • Last Updated : 27 Apr, 2020

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:






<!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:




My Personal Notes arrow_drop_up
Recommended Articles
Page :