Lodash - Get index of first occurence of an array value

To get the array values index based on it first occurrence in the array, you can use _.indexOf() lodash method.

const names = ["Ankit", "Rick", "Maggie", "John", "Wick", "Rick", "Carol", "Maggie"];

//Get index of Rick
const rick_index = _.indexOf(names, 'Rick');
// => 1

//Get Index of Maggie start from - 3
const maggie_index = _.indexOf(names, 'Maggie', 3);
// => 7

//Get Index of Maggie start from - 1
const maggie_index_1 = _.indexOf(names, 'Maggie', 1);
// => 2

_.indexOf() Lodash

_.indexOf(Array, Value, FromIndex=0)

Array: [Required]

Value: [Required]

FromIndex: [Optional]

Example Demo

Was this helpful?