how to use the indexof function from the lodash library in typescript

To use the indexOf function from the Lodash library in TypeScript, you can start by importing the function from the library as shown below:

index.ts
import { indexOf } from 'lodash';
34 chars
2 lines

The indexOf function is now available to use, and its first argument is the array to search for the given search value. The second argument is the value to search for in the array, and the optional third argument is the index at which to begin the search (defaults to 0).

Here's an example of using the indexOf function in TypeScript:

index.ts
const cities: string[] = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Philadelphia'];

const searchIndex = indexOf(cities, 'Chicago');
console.log(searchIndex); // 2
172 chars
5 lines

In this example, we use the indexOf function to search for the index of the 'Chicago' value in the 'cities' array. The returned value is 2, since 'Chicago' is at the 2nd index of the array.

gistlibby LogSnag