javascript
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?
Similar Posts
- Lodash - get the first element from an array
- Lodash - Get index of array item based on some condition
- Remove first n elements from an array using Lodash
- Lodash - Find item index from array by searching from the end
- Get all values from an array which does not exist in other array Lodash
- Find the index of Max value in an array using Javascript
- Replace array values with another value Lodash