how to use the lastindexof function from the underscore library in typescript

To use the lastIndexOf function from the Underscore library in TypeScript, you will need to import the Underscore library as follows:

index.ts
import * as _ from 'underscore';
33 chars
2 lines

Then, you can use the lastIndexOf function to find the last index of a specified value in an array, as follows:

index.ts
const arr = [1, 2, 3, 4, 3, 2, 1];
const lastIndexOfThree = _.lastIndexOf(arr, 3);
console.log(lastIndexOfThree); // Output: 4
127 chars
4 lines

In the code above, we imported Underscore library and assigned it to a variable _. Then, we created an array and assigned it to a variable 'arr'. We called the lastIndexOf function, passing in the arr array and the value we want to find the last index of which is '3'. The function returns the last index of the specified value in the array.

gistlibby LogSnag