Saturday, September 21, 2024
Google search engine
HomeLanguagesJavascriptLodash _.sortedLastIndexBy() Method

Lodash _.sortedLastIndexBy() Method

The _.sortedLastIndexBy() method is used to return the highest 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 array to compute their sort ranking.

Syntax:

_.sortedLastIndexBy(array, value, [iteratee=_.identity])

Parameters: This method accepts three parameters as mentioned above and described below:

  • array: This parameter holds the sorted array.
  • value: This parameter holds the value to evaluate.
  • Iteratee: This is the function that iterates over each element.

Return Value: This method returns the index at which the value should be inserted into the array.

Example 1: Here, const _ = require(‘lodash’) is used to import the lodash library into the file.

Javascript




// Requiring the lodash library 
const _ = require("lodash"); 
    
// Original array 
var objects = [{ 'x': 4 }, { 'x': 6 }];
    
// Use of _.sortedLastIndexBy() 
// method 
let index = _.sortedLastIndexBy(objects, 
    { 'x': 5 }, function(o) { return o.x; });
    
// Printing the output 
console.log(index);


Output:

1

Example 2:

Javascript




// Requiring the lodash library 
const _ = require("lodash"); 
    
// Original array 
var objects = [{ 'x': 4 }, { 'x': 6 }];
    
// Use of _.sortedLastIndexBy() 
// method 
let index = _.sortedLastIndexBy(
        objects, { 'x': 9 }, 'x');
    
// Printing the output 
console.log(index);


Output:

2

Example 3:

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.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments