VOOZH about

URL: https://www.geeksforgeeks.org/javascript/lodash-_-sortedindexof-method/

⇱ Lodash _.sortedIndexOf() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Lodash _.sortedIndexOf() Method

Last Updated : 2 Sep, 2024

Lodash _.sortedIndexOf() method is used to get the index of the first occurrence of the particular element in the sorted array. It uses the binary search to sort an array.

Syntax:

_.sortedIndexOf(array, value);

Parameters:

  • 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, other returns -1.

Example 1: In this example, we are getting the index at which we got the given value in the given array.

Output:

3

Example 2:In this example, we are getting the index at which we got the given value in the given array.

Output:

4

Example 3: In this example, we are getting -1 as the index because the given value is not present in the given array.

Output:

-1

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

Comment