Open In App
Related Articles

Lodash _.sortedLastIndexOf() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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:
 

  • array: This parameter holds the array to inspect.
  • value: This parameter holds the value to search for.


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.
 

javascript

// 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: 
 

javascript

// 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


 



Last Updated : 19 Aug, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads