Last Updated : 02 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:
Example 1: In this example, we are getting the index at which we got the given value in the given array.
JavaScript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let x = [1, 2, 3, 4, 4, 4, 5, 6, 6]
// Use of _.sortedIndexOf()
// method
let index = _.sortedIndexOf(x, 4);
// Printing the output
console.log(index);
Output:
3
Example 2:In this example, we are getting the index at which we got the given value in the given array.
JavaScript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let x = ['a', 'b', 'c', 'd', 'e', 'e', 'e', 'f']
// Use of _.sortedIndexOf()
// method
let index = _.sortedIndexOf(x, 'e');
// Printing the output
console.log(index);
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.
JavaScript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let x = ['ajax', 'django', 'mongoDb',
'react', 'reactnative', 'yarn']
// Use of _.sortedIndexOf()
// method
let index = _.sortedIndexOf(x, 'luby');
// Printing the output
console.log(index);
Output:
-1
Note: This will not work in normal JavaScript because it requires the library lodash to be installed.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4