Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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.
Basic Usage:
4.2.
JavaScript
4.3.
Object Array:
4.4.
JavaScript
4.5.
Custom Predicate Function:
4.6.
JavaScript
4.7.
Specifying Start Index:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.findLastIndex() differ from _.findIndex()?
5.2.
Can this method handle nested array structures?
5.3.
Is _.findLastIndex() efficient for large arrays?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.findLastIndex() Method

Author Pallavi singh
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

In the realm of array manipulation in JavaScript, the ability to pinpoint the position of an element from the end of the array based on certain criteria is invaluable. Lodash's _.findLastIndex() method fills this need, offering an efficient way to find the index of the last element in the array that satisfies a given condition. 

Lodash _.findLastIndex() Method

This function is a testament to Lodash's capability in enhancing the ease and effectiveness of working with arrays, especially when dealing with reverse searches in complex data sets.

Why This Function is Used

The _.findLastIndex() method is particularly useful in scenarios like:

  • Reverse Searching: Locating the last occurrence of an element in an array that meets a specific condition.
     
  • Data Analysis: Useful in analyzing data from the end, such as the most recent records in time-based data.
     
  • User Interface Logic: In UIs where the most recent interactions or changes are prioritized.
     
  • Complex Data Structures: Navigating through arrays of objects or nested arrays from the end.
     

This method enhances the efficiency and readability of code that requires reverse searching in arrays.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax, Parameter, and Return Value

Syntax:

_.findLastIndex(array, [predicate=_.identity], [fromIndex=array.length-1])

Parameters:

  • array (Array): The array to inspect.
     
  • predicate (Function): The function invoked per iteration.
     
  • fromIndex (optional, number): The index to search from, default is the array's length minus one.

Return Value:

The index of the last element predicate returns truthy for, else -1.

Examples

Basic Usage:

Finding the last index of a positive number.

  • JavaScript

JavaScript

let numbers = [-1, 0, 1, 2, 3, -2, -3];

let index = _.findLastIndex(numbers, (n) => n > 0);

console.log(index);

Output: 

4

Object Array:

Finding the last index where an object property matches a value.

  • JavaScript

JavaScript

let users = [

 { 'user': 'barney',  'active': true },

 { 'user': 'fred',  'active': false },

 { 'user': 'pebbles', 'active': true }

];

let index = _.findLastIndex(users, { 'user': 'pebbles', 'active': true });

console.log(index);

 Output: 

2

Custom Predicate Function:

Using a custom function to find the last index of an element.

  • JavaScript

JavaScript

let numbers = [1, 2, 3, 4, 5];

let index = _.findLastIndex(numbers, (n) => n % 2 === 0);

console.log(index);

Output: 

3

Specifying Start Index:

Starting the search from a specific index in reverse.

  • JavaScript

JavaScript

let numbers = [1, 2, 3, 4, 5, 3];

let index = _.findLastIndex(numbers, (n) => n === 3, 4);

console.log(index);

Output:

2

Frequently Asked Questions

How does _.findLastIndex() differ from _.findIndex()?

The _.findLastIndex() searches from the end of the array, while _.findIndex() searches from the beginning.

Can this method handle nested array structures?

Yes, with an appropriately defined predicate function, it can navigate and evaluate nested arrays or objects.

Is _.findLastIndex() efficient for large arrays?

Yes, it is efficient for large arrays, particularly because it starts from the end, reducing iteration time for reverse searching.

Conclusion

The _.findLastIndex() method in Lodash is a key tool for reverse searching in arrays. It is especially beneficial in scenarios where the latest or the last occurrences are of interest. This method's ability to seamlessly integrate into JavaScript code, offering simplicity and efficiency, makes it a preferred choice for developers dealing with complex arrays and data structures.

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.

Previous article
Lodash _.flatten() Method
Next article
Lodash _.forEachRight() Method
Live masterclass