Open In App

Lodash _.sortedLastIndexOf() Method

The _.sortedLastIndexOf method is used to return the highest index of the array where an element can be inserted and maintain its sorted order. Also this method is like _.lastIndexOf except that it performs a binary search on a sorted array.
Syntax: 
 

_.sortedLastIndexOf(array, value)

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



Return Value: It returns the index of the matched value, else -1.
Example 1: Here, const _ = require(‘lodash’) is used to import the lodash library into the file.
 




// Requiring the lodash library  
const _ = require("lodash");  
      
// Original array  
let y = [4, 5, 7, 7, 7]   
      
// Use of _.sortedLastIndexOf()  
// method  
let index = _.sortedLastIndexOf(y, 7); 
      
// Printing the output  
console.log(index);

Output: 
 



4

Example 2: 
 




// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
let y = ['p', 'q', 'r', 't', 't', 'u', 's', 't'
      
// Use of _.sortedLastIndexOf() 
// method 
let index = _.sortedLastIndexOf(y, 't'); 
      
// Printing the output 
console.log(index);

Output: 
 

7

 

Article Tags :