Open In App

Underscore.js _.find() Function

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:

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.




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




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

Article Tags :