Lodash _.find() Method

What is _.find() method?

This method is used to search for any element or the first element which satisfies some condition. If we have an object or array, we want to know the first element of the array that satisfies the given condition. Then we will apply this function.

Syntax:

This function accepts three arguments in which two arguments are mandatory, and one argument is optional.

  • object: This argument takes the array or an object from which we want to search the element.
  • condition: This is the condition or the function which takes each value of the array and evaluates its value. If the element satisfies the condition, then it returns it to the resultant variable.
  • startingIndex: This is an optional parameter where this function starts searching for elements from this index instead from the beginning of the list.

Return value: This function will return the first element from the list which satisfies the given condition. If no element exists, then it will return undefined.

Now, we will see various examples of the _.find() method:

Example 1:

Javascript code:

Output:

13

Explanation:

In the above code, we have imported the lodash library using require keyword. Then we created an integer list to use the _.find() method. Then we applied the _.find() method on the given list, and in the condition part, we used a function that takes one number. If the square of the given number is greater than 100, then it will return true.

In this function, each value of the list is passed, and we got the first value, 13, which has a square greater than 100, so it will store the 100 in the result variable. Now we printed the result value using the console.log() statement.

Example2:

Javascript code:

Output:

29

Explanation:

In the above code, we have imported the lodash library using require keyword. Then we created an integer list to use the _.find() method. Then we applied the _.find() method on the given list, and in the condition part, we used a function that takes one number. If the number is greater than ten, then it will return true, but we used the third parameter of the index also.

So in the above example, the first element, which is greater than 10, is 15, but it has an index value as1, so it will not be considered. Then we have the next value as 29, which is greater than ten, and its index value is also greater than 2. So we got the result as 29.

Example 3:

Javascript Code:

Output:

{name: 'Ayush', marks:'85'}

Explanation:

In the above code, we have imported the lodash library using require keyword. Then we created an object to use the _.find() method. Then we applied the _.find() method on the given list, and in the condition part, we used a function that takes one object. If the object's marks are greater than or equal to 85, then it returns true. So Ayush has 85 marks, and it satisfies the condition, and we got the result as a list, and we printed it using the console.log() statement.

Example 4:

Javascript code:

Output:

undefined

Explanation:

In the above code, we find out the element is less than -90, but it does not exist. So, we got the answer as undefined.






Latest Courses