Last Updated : 02 Sep, 2024
Lodash _.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.
Syntax:findLastIndex(array, [predicate=_.identity], fromIndex);Parameters:
It returns the index of the element if found else -1 is returned.
Example 1: In this example, we are getting the last index of element 2 that is present in the given array by the use of the lodash _.findLastIndex() function.
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let array1 = [4, 2, 3, 1, 4, 2]
// Using lodash.findLastIndex
let index = _.findLastIndex(array1, (e) => {
return e == 2;
});
// Original Array
console.log("original Array: ", array1)
// Printing the index
console.log("index: ", index)
Output:
Example 2: In this example, we are using the lodash _.findLastIndex() functionand getting "-1" as the element 1 is not present in the given array.
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let array1 = [4, 2, 3, 1, 4, 2]
// Using lodash.findLastIndex
let index = _.findLastIndex(array1, (e) => {
return e == 1;
}, 2);
// Original Array
console.log("original Array: ", array1)
// Printing the index
console.log("index: ", index)
Output:
Example 3: In this example, we are getting the last index of element 2 that is present in the given array of objects by the use of the lodash _.findLastIndex() function.
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let array1 = [
{ "a": 1, "b": 2 },
{ "b": 4 },
{ "a": 1 }
]
// Using lodash.findLastIndex
let index = _.findLastIndex(array1, (e) => {
return e.b == 2;
}, 2);
// Original Array
console.log("original Array: ", array1)
// Printing the index
console.log("index: ", index)
Output:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4