Lodash | _.find() Method

The _.find() method accessing each value of the collection and returns the first element that passes a truth test for the predicate or undefined if no value passes the test. The function returns as soon as it finds the match. So it actually searches for elements according to the predicate.

Syntax:

_.find(collection, predicate, fromIndex)

Parameters: This method accept three parameters as mentioned above and described below:

  • collection: This parameter holds the array or obect collection that need to inspect.
  • predicate: This parameter holds the function invoked the iteration.
  • fromIndex: This parameter holds the index from which you want to start searching (optional). If you don’t pass this parameter then it start searching from the beginning.

Return Value: It returns the matched element or undefined if nothing match.

Example 1: Tn this example, we will try to find the first number whose square is more than 100.



filter_none

edit
close

play_arrow

link
brightness_4
code

const _ = require('lodash');
  
let x = [2, 5, 7, 10, 13, 15];
  
let result = _.find(x, function(n) {
    if (n * n > 100) {
        return true;
    }
});
  
console.log(result);

chevron_right


Here, const _ = require('lodash') is used to import the lodash library into the file.

Output:

13

Example 2: In this example, we will find the first number in the list which is greater than 10 but start searching from index 2.

filter_none

edit
close

play_arrow

link
brightness_4
code

const _ = require('lodash');
  
let x = [-1, 29, 7, 10, 13, 15];
  
let result = _.find(x, function(n) {
    if (n > 10) {
        return true;
    }
}, 2);
  
console.log(result);

chevron_right


Output:

13

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

filter_none

edit
close

play_arrow

link
brightness_4
code

const _ = require('lodash');
  
let x = [
    {'name': 'Akhil', marks:'78'}, 
    {'name': 'Akhil', marks:'98'}, 
    {'name': 'Akhil', marks:'97'}
]; 
  
let result = _.find(x, function(obj) {
    if (obj.marks > 90) {
        return true;
    }
});
  
console.log(result);

chevron_right


Output:

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

Example 4: When none element return true on predicate.

filter_none

edit
close

play_arrow

link
brightness_4
code

const _ = require('lodash');
  
let x = [1, 2, 7, 10, 13, 15];
  
let result = _.find(x, function(n) {
    if (n < 0) {
        return true;
    }
});
  
console.log(result);

chevron_right


Output:

undefined

Note: This will not work in normal JavaScript because it requires the library lodash to be installed.

Reference: https://lodash.com/docs/4.17.15#find

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.