Chrome Extension

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
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

_.indexOf() Lodash

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

Array: [Required]

Value: [Required]

FromIndex: [Optional]

Example Demo

Search Index Data (The code snippet can also be found with below search text)

_.indexOf() lodash
Was this helpful?
0 Comments
Join Devsheet Ask a Question