Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples
4.1.
Finding Last Matching Number:
4.2.
JavaScript
4.3.
Searching for the Last Object with a Specific Property:
4.4.
JavaScript
4.5.
Finding the Last Element in an Object:
4.6.
JavaScript
4.7.
Using a Property Name Shorthand:
4.8.
JavaScript
5.
Frequently Asked Questions 
5.1.
How does _.findLast() compare with _.find()?
5.2.
Can _.findLast() be used with a negative fromIndex?
5.3.
Is _.findLast() suitable for searching large collections?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.findLast() Method

Author Riya Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaScript programming often requires searching through collections, not just from the start, but sometimes from the end. Lodash caters to this need with its _.findLast() method. This function is designed to locate the last element in a collection that satisfies a provided predicate. 

Lodash _.findLast() Method

In this article, we'll explore _.findLast() in detail, providing insights into its syntax, use cases, and practical applications, backed by examples and FAQs.

Why This Function is Used

The _.findLast() function is essential when the focus is on the most recent or last occurrence of an element that meets certain criteria within a collection. This is particularly useful in scenarios where the latest or last matching element has more relevance, such as in log parsing, historical data analysis, or when dealing with reverse-ordered collections. It offers a convenient and efficient way to perform such searches, negating the need for reverse iteration or manual looping.

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

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

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

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

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass