The _.findWhere() function is used to have a list of all the elements that matches the given property. The _.findWhere() function is used to search a content in the whole list of sections. The section which contains the content will be displayed.
Parameters: It takes two arguments:
- List: This parameter contains the list of items.
- Property: This parameter contains the test condition.
Return value: The details of the element selected from the list are returned. Only the first element matched will be given as output.
Difference between _.findWhere() and _.where() function: Both the functions takes an array name and the property to match but the _.where() function displays all the matches whereas, _.findWhere) function matches only the first match.
Searching a property in an array: The ._findWhere() function takes the array elements one by one and matches the given property is same or not. If the property matches it displays the rest of the details of that particular element. After the property is matched the first time the _.findWhere() function ends. It only displays the first match.
Passing a list of elements with a number of different properties to _.findWhere() function: Firstly, declare the array with all the elements and their specific properties. Then pass the array name along with the property which needs to match to the _.findWhere() function. All the rest properties will be displayed as output of that specific element.
Passing an array with ‘true/false’ as a property to the _.findWhere() function: First declare the array (here array is ‘people’). It is the property (here, ‘hasLong’) be defines as ‘true’ or ‘false’. Choose one condition to check like here ‘hasLongHairs’. Console.log display the final answer.
Passing an array of numbers as property to _.findWhere() function together: It also follows the same procedure of firstly declaring the array with all the properties and the giving the array name and the property of matching to the _.findWhere() function.
- Underscore.js | every() with Examples
- Underscore.js | contains() with Examples
- jQuery | eq() with Examples
- Underscore.js | where() with Examples
- jQuery | last() with Examples
- jQuery | first() with Examples
- Underscore.js | map() with Examples
- jQuery | val() with Examples
- jQuery | after() with Examples
- jQuery | on() with Examples
- jQuery | one() with Examples
- Underscore.js | _.last() with Examples
- jQuery | has() with Examples
- Underscore.js | max() with Examples
- Underscore.js | min() with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.