Open In App

Underscore.js _.find() Function

Improve
Improve
Like Article
Like
Save
Share
Report

Underscore.js _.find() function looks at each element of the list and returns the first occurrence of the element that satisfies the condition. If any element of the list does not satisfy the condition then it returns the undefined value.

Syntax:

_.find(list, predicate, [context])

Parameters:

  • 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 that needs to be displayed. It is an optional parameter.

Return value:

It returns the first occurrence of the element that satisfies the condition.

Example 1: In this example, we are using the Underscore.js _.find() function.

html




<!DOCTYPE html>
<html>
 
<head>
    <script type="text/javascript" src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        let oddNo = _.find([5, 6, 7, 8, 9, 10],
            function (num) {
                return num % 2 != 0;
            });
        console.log(oddNo);
    </script>
</body>
 
</html>


Output: It will be shown in the console.

5

Example 2: In this example, we are using the Underscore.js _.find() function.

html




<!DOCTYPE html>
<html>
 
<head>
    <script type="text/javascript" src=
    </script>
</head>
 
<body>
    <script type="text/javascript">
        let words = ['javascript', 'java', 'unix',
                    'hypertext', 'underscore', 'CSS'];
 
        const result = words.find(word => word.length == 9);
        console.log(result);
    </script>
</body>
 
</html>


Output: It will be shown in the console.

hypertext


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