Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

lodash
communitycreator

What is the _.lastIndexOf() method in Lodash?

Shubham Singh Kshatriya

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The _.lastIndexOf() method in Lodash is used to get the index of the first occurrence of a value from an array. This method iterates over the values of an array from end to start.

Syntax

_.lastIndexOf(array, value, startIndex)

Parameter values

This method accepts the following parameters:

  • array: This is the array that needs to be queried.
  • value: This is the value that needs to be searched for.
  • startIndex: This is the index from where the search starts. If not specified, it takes the length of the array as the default value.

Return value

This method returns the index of the value if found. Otherwise, it returns -1.

Code

Let’s look at an example of the _.lastIndexOf() method in the code snippet below:

Console
Implementation of the "_.lastIndexOf()" method

Explanation

In the HTML tab:

  • Line 5: We import the lodash script.

In the JavaScript tab:

  • Line 2: We create an array called numbers and populate it with a few values.
  • Line 5: We declare a variable called value and initialize it with the value 3.
  • Line 8: We use the _.lastIndexOf() method to find the index of the value.
  • Line 11: We print the index to the console.

Output

In the output, we will see 4, which is the index of the value 3 in the array numbers from its end.

RELATED TAGS

lodash
communitycreator

CONTRIBUTOR

Shubham Singh Kshatriya

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring