Lodash _.indexOf() Method
lodash _.indexOf() method is used to get the index of the first occurrence of the particular element in the array. If fromIndex is not present in the array negative one is given as the output and no error is displayed.
Syntax:
indexOf(array, value, fromIndex);
Note:
If the value is not found in the array -1 is returned.
Parameters:
- array: It is the array in which value is to be found.
- value: It is the value to be looked at in the array.
- fromIndex: It is the index after which we have to look for the value.
Return Value:
It returns the index of the value in the array. If the value is not found, the array returns -1.
Example 1: In this example, we are getting the index at which the given value is present in the given array.
Javascript
const _ = require( "lodash" );
let array = [1, 2, 3, 4]
console.log( "Array : " , array)
let index = _.indexOf(array, 3, 0)
console.log( "Index : " , index)
|
Output:
Example 2: In this example, we are getting -1 as the index becuase the given value is not present after the given index in the given array.
Javascript
const _ = require( "lodash" );
let array = [1, 2, 3, 4]
console.log( "Array : " , array)
let index = _.indexOf(array, 3, 3)
console.log( "Index : " , index)
|
Output:
Last Updated :
03 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...