Wednesday, July 3, 2024
HomeLanguagesJavascriptLodash _.sortedIndex() Method

Lodash _.sortedIndex() Method

The _.sortedIndex() method is used to return the lowest index of the array where an element can be inserted and maintain its sorted order. It uses the binary search.

Syntax:

_.sortedIndex(array, value)

Parameters: This method accept 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.

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 _.sortedIndex() 
// method 
let index = _.sortedIndex(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 _.sortedIndex() 
// method 
let index = _.sortedIndex(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 _.sortedIndex() 
// method 
let index = _.sortedIndex(x, 'ruby');
    
// Printing the output 
console.log(index);


Output:

5

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!

Nango Kalahttps://www.kala.co.za
Experienced Support Engineer with a demonstrated history of working in the information technology and services industry. Skilled in Microsoft Excel, Customer Service, Microsoft Word, Technical Support, and Microsoft Office. Strong information technology professional with a Microsoft Certificate Solutions Expert (Privet Cloud) focused in Information Technology from Broadband Collage Of Technology.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments