Lodash _.sortedLastIndexOf() Method
Last Updated :
19 Aug, 2020
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
const _ = require( "lodash" );
let y = [4, 5, 7, 7, 7]
let index = _.sortedLastIndexOf(y, 7);
console.log(index);
|
Output:
4
Example 2:
javascript
const _ = require( "lodash" );
let y = [ 'p' , 'q' , 'r' , 't' , 't' , 'u' , 's' , 't' ]
let index = _.sortedLastIndexOf(y, 't' );
console.log(index);
|
Output:
7
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...