VOOZH about

URL: https://www.geeksforgeeks.org/javascript/lodash-_-find-method/

⇱ Lodash _.find() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Lodash _.find() Method

Last Updated : 21 Aug, 2024

The Lodash _.find() method searches through a collection (array or object) and returns the first element that satisfies a specified condition (predicate). It’s useful for quickly locating a matching item within a collection. If no match is found, it returns undefined.

Syntax

_.find(collection, predicate, [fromIndex=0])

Parameters:

  • collection: This parameter holds the array or object collection that needs to be inspected.
  • predicate: This parameter holds the function invoked in each iteration.
  • fromIndex: This parameter holds the index from which you want to start searching (optional). By default it's 0. 

Return Value:

It returns the matched element or undefined if nothing matches.

Example: In this example, we are passing the array and a function in the _.find() method which returns the matched first value.

Output:

2

Example 2: In this example, we will search for the first student (object) in the list who has more scores than 90.  

Output:

{name: 'Akhil', marks: '98' }

Example 3: In this example, we are getting 'undefined' as there is no matched value present in the array according to the function.

Output:

undefined
Comment