Syntax, Parameter and Return Value
Syntax:
_.findLast(collection, [predicate=_.identity], [fromIndex=collection.length-1])
Parameters:
-
collection (Array|Object): The collection to inspect.
-
[predicate=_.identity] (Function): The function invoked per iteration.
- [fromIndex=collection.length-1] (number): The index to start the search from, backward.
Return Value:
Returns the matched element, else undefined.
Examples
Finding Last Matching Number:
JavaScript
var _ = require('lodash');
var numbers = [1, 2, 3, 4, 5, 3, 6];
var lastThree = _.findLast(numbers, n => n === 3);
console.log(lastThree);

You can also try this code with Online Javascript Compiler
Run Code
Output:
3
This example illustrates finding the last occurrence of the number 3 in an array.
Searching for the Last Object with a Specific Property:
JavaScript
var users = [{ 'user': 'barney', 'active': true },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }];
var lastActiveUser = _.findLast(users, { 'active': true });
console.log(lastActiveUser);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'user': 'pebbles', 'active': true }
Demonstrates locating the last object in an array that has a specified property value.
Finding the Last Element in an Object:
JavaScript
var object = { 'a': 1, 'b': 2, 'c': 3, 'd': 2 };
var lastTwo = _.findLast(object, v => v === 2);
console.log(lastTwo);

You can also try this code with Online Javascript Compiler
Run Code
Output:
2
Shows how to find the last occurrence of a value in an object.
Using a Property Name Shorthand:
JavaScript
var products = [{ 'name': 'apple', 'inStock': false },
{ 'name': 'banana', 'inStock': true },
{ 'name': 'pear', 'inStock': true }];
var lastInStock = _.findLast(products, 'inStock');
console.log(lastInStock);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'name': 'pear', 'inStock': true }
Utilizes Lodash's shorthand syntax for simpler and more concise code.
Frequently Asked Questions
How does _.findLast() compare with _.find()?
While _.find() returns the first element that meets the criteria, _.findLast() searches from the end of the collection, returning the last matching element.
Can _.findLast() be used with a negative fromIndex?
Yes, a negative fromIndex starts the search from the end of the collection, moving backwards.
Is _.findLast() suitable for searching large collections?
It is suitable, but the efficiency depends on the collection size and predicate complexity; it may be less efficient for very large collections.
Conclusion
Lodash's _.findLast() method is a highly useful tool for situations where the last occurrence of a matching element in a collection is of interest. It extends the functionality of searching in arrays and objects, offering a more targeted and efficient approach, especially in scenarios where the most recent data is most relevant.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.