A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/lodash-_-sortedindexby-method/ below:

Lodash _.sortedIndexBy() Method - GeeksforGeeks

Lodash _.sortedIndexBy() Method

Last Updated : 02 Sep, 2024

Lodash _.sortedIndexBy() method is used to return the lowest index of the array where an element can be inserted and maintain its sorted order. In addition, it accepts iteratee which is invoked for value and each element of the array to compute their sort ranking. It uses the binary search.

Syntax:
_.sortedIndexBy(array, value, [iteratee=_.identity]);
Parameters: Return Value:

Example 1: In this example, we are getting the index at which we can insert the given value.

JavaScript
// Requiring the lodash library 
const _ = require("lodash");

// Original array 
let objects = [{ 'x': 4 }, { 'x': 6 }];

// Use of _.sortedIndexBy() 
// method 
let index = _.sortedIndexBy(objects,
    { 'x': 5 }, function (o) { return o.x; });

// Printing the output 
console.log(index); 

Output:

1

Example 2: In this example, we are getting the index at which we can insert the given value.

JavaScript
// Requiring the lodash library 
const _ = require("lodash");

// Original array 
let objects = [{ 'x': 4 }, { 'x': 6 }];

// Use of _.sortedIndexBy() 
// method 
let index = _.sortedIndexBy(objects, { 'x': 9 }, 'x');

// Printing the output 
console.log(index); 

Output:

2

Example 3: In this example, we are getting the index at which we can insert the given value.

JavaScript
// Requiring the lodash library 
const _ = require("lodash");

// Original array 
let x = ['ajax', 'django', 'mongoDb',
    'react', 'reactnative', 'yarn']

// Use of _.sortedIndexBy() 
// method 
let index = _.sortedIndexBy(x, 'luby', 5);

// Printing the output 
console.log(index); 

Output:

3

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