Last Updated : 02 Sep, 2024
lodash _.indexOf() method is used to get the index of the first occurrence of the particular element in the array. If fromIndex is absent in the array, a 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:
Example 1: In this example, we are getting the index at which the given value is present in the given array.
JavaScript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let array = [1, 2, 3, 4]
// Printing original array
console.log("Array : ", array)
// Looking for value 3 from index 0
let index = _.indexOf(array, 3, 0)
// Printing the Index of the value
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
// Requiring the lodash library
const _ = require("lodash");
// Original array
let array = [1, 2, 3, 4]
// Printing original array
console.log("Array : ", array)
// Looking for 3 from index 3
// it will return -1
let index = _.indexOf(array, 3, 3)
// Print Index of the value
console.log("Index : ", index)
Output:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4