Skip to content
Related Articles

Related Articles

Lodash | _.sortedLastIndex() Method
  • Last Updated : 08 May, 2020

The _.sortedLastIndex() method is used to return the highest index of the array where an element can be inserted and maintain its sorted order.

Syntax:

_.sortedLastIndex(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:






const _ = require('lodash');
  
let x = [1, 2, 3, 4, 4, 4, 5, 6, 6]
  
let index = _.sortedLastIndex(x, 4);
  
console.log(index);

Here, const _ = require('lodash') is used to import the lodash library into the file.

Output:

6

Example 2:




const _ = require('lodash');
  
let x = ['a', 'b', 'c', 'd', 'e', 'e', 'e', 'f']
  
let index = _.sortedLastIndex(x, 'e');
  
console.log(index);

Output:

7

Example 3:




const _ = require('lodash');
  
let x = ['ajax', 'django', 'mongoDb'
       'react', 'reactnative', 'yarn']
  
let index = _.sortedLastIndex(x, 'ruby');
  
console.log(index);

Output:

5

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

Reference: https://lodash.com/docs/4.17.15#sortedLastIndex

My Personal Notes arrow_drop_up
Recommended Articles
Page :