Thursday, September 4, 2025
HomeLanguagesJavascriptLodash _.sortedIndexOf() Method

Lodash _.sortedIndexOf() Method

The _.sortedIndexOf() method is used to get the index of first occurrence of the particular element in the sorted array. It uses the binary search to sort an array.

Syntax:

_.sortedIndexOf(array, value)

Parameters: This method accepts two parameters as mentioned above and described below:

  • array: This parameter holds the sorted array.
  • value: This parameter holds the value to evaluate.

Return Value: This method returns the index at which the value should be inserted into the array, other return -1.

Example 1: Here, const _ = require(‘lodash’) is used to import the lodash library into the file.

Javascript




// Requiring the lodash library 
const _ = require("lodash"); 
    
// Original array 
let x = [1, 2, 3, 4, 4, 4, 5, 6, 6]  
    
// Use of _.sortedIndexOf() 
// method 
let index = _.sortedIndexOf(x, 4);
    
// Printing the output 
console.log(index);


Output:

3

Example 2:

Javascript




// Requiring the lodash library 
const _ = require("lodash"); 
    
// Original array 
let x = ['a', 'b', 'c', 'd', 'e', 'e', 'e', 'f']  
    
// Use of _.sortedIndexOf() 
// method 
let index = _.sortedIndexOf(x, 'e');
    
// Printing the output 
console.log(index);


Output:

4

Example 3:

Javascript




// Requiring the lodash library 
const _ = require("lodash"); 
    
// Original array 
let x = ['ajax', 'django', 'mongoDb',  
       'react', 'reactnative', 'yarn']  
    
// Use of _.sortedIndexOf() 
// method 
let index = _.sortedIndexOf(x, 'luby');
    
// Printing the output 
console.log(index);


Output:

-1

Note: This will not work in normal JavaScript because it requires the library lodash to be installed.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6746 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS