Lodash _.findLastIndex() Function
The _.findLastIndex() function is used to find the element from the right of the array. Therefore giving the index of the last occurrence of the element in the array.
findLastIndex(array, [predicate=_.identity], fromIndex);
- array: It is the original array.
- predicate: It is the function that iterates over each element.
- fromIndex: It is the index after which the searching starts. If from Index is not given then by default it is n-1 where n is the length of the array.
Return Value: It returns the index of the element if found else -1 is returned.
Note: Install lodash module by using command
npm install lodash before using the code given below.
Example 2: When an element is present in the array but the output is -1 because it is present after the from the index. Here fromIndex is 2.
Example 3: When an array of objects is given.