Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Lodash _.indexOf() Method

  • Last Updated : 22 Jul, 2020

The lodash _.indexOf() method is used to get the index of 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:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

indexOf(array, value, fromIndex)

Note: If the value is not found in the array -1 is returned.

Parameters: This method accepts three parameters as mention above and describe below.



  • array: It is the array in which value is to be found.
  • value: It is the value to be looked 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 returned -1.

Example 1:

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: Looking for a value from particular index. 

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:




My Personal Notes arrow_drop_up
Recommended Articles
Page :