Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Lodash _.sortedLastIndexOf() Array Method

Posted in lodash Tutorial
Updated on Feb 24, 2024
By Mari Selvan
👁️ 28 - Views
⏳ 4 mins
💬 1 Comment
Lodash _.sortedLastIndexOf() Array Method

Photo Credit to CodeToFun

🙋 Introduction

Sorting arrays is a common task in JavaScript, and Lodash provides a powerful tool for this with the _.sortedLastIndexOf() method.

This method allows developers to efficiently find the index of the last occurrence of a value in a sorted array. This can be invaluable when working with ordered data sets and searching for specific elements.

🧠 Understanding _.sortedLastIndexOf()

The _.sortedLastIndexOf() method in Lodash performs a binary search on a sorted array to find the index of the last occurrence of a specified value.

💡 Syntax

syntax.js
Copied
Copy To Clipboard
_.sortedLastIndexOf(array, value)
  • array: The sorted array to search.
  • value: The value to search for.

📝 Example

Let's dive into a practical example to illustrate the usage of _.sortedLastIndexOf():

example.js
Copied
Copy To Clipboard
// Include Lodash library (ensure it's installed via npm)
const _ = require('lodash');

const sortedArray = [1, 2, 2, 2, 3, 4, 5];
const targetValue = 2;
const lastIndex = _.sortedLastIndexOf(sortedArray, targetValue);

console.log(lastIndex);
// Output: 3

In this example, the sortedArray is searched for the last occurrence of the value 2, and the index 3 is returned.

🏆 Best Practices

  1. Use with Sorted Arrays:

    Ensure that the input array is sorted before using _.sortedLastIndexOf(). The method's efficiency relies on the array being in ascending order.

    example.js
    Copied
    Copy To Clipboard
    const unsortedArray = [3, 1, 4, 2, 5];
    const unsortedIndex = _.sortedLastIndexOf(unsortedArray, 2);
    
    console.log(unsortedIndex);
    // Output: -1 (Incorrect result on an unsorted array)
  2. Handle Non-Existent Values:

    Handle cases where the target value does not exist in the array. The method returns -1 when the value is not found.

    example.js
    Copied
    Copy To Clipboard
    const nonExistentValue = 6;
    const nonExistentIndex = _.sortedLastIndexOf(sortedArray, nonExistentValue);
    
    console.log(nonExistentIndex);
    // Output: -1
  3. Leverage Binary Search Efficiency:

    Take advantage of the binary search algorithm employed by _.sortedLastIndexOf() for efficient searches in large sorted arrays.

    example.js
    Copied
    Copy To Clipboard
    const largeSortedArray = [...Array(100000).keys()];
    const targetValue = 50000;
    const efficientIndex = _.sortedLastIndexOf(largeSortedArray, targetValue);
    
    console.log(efficientIndex);
    // Output: 50000

📚 Use Cases

  1. Finding Last Occurrence:

    The primary use case for _.sortedLastIndexOf() is finding the index of the last occurrence of a value in a sorted array.

    example.js
    Copied
    Copy To Clipboard
    const lastOccurrenceIndex = _.sortedLastIndexOf(sortedArray, 2);
    console.log(lastOccurrenceIndex);
    // Output: 3
  2. Search in Ranges:

    Use this method to find the last index within a specific range of a sorted array.

    example.js
    Copied
    Copy To Clipboard
    const rangeStart = 1;
    const rangeEnd = 4;
    const lastIndexInRange = _.sortedLastIndexOf(sortedArray, 2, rangeStart, rangeEnd);
    
    console.log(lastIndexInRange);
    // Output: 3

🎉 Conclusion

The _.sortedLastIndexOf() method in Lodash is a valuable addition to the toolkit of any JavaScript developer working with sorted arrays. Its efficiency and ease of use make it an excellent choice for tasks involving the identification of the last occurrence of a value. By incorporating this method into your code, you can enhance both the performance and reliability of your array-related operations.

Explore the capabilities of _.sortedLastIndexOf() in Lodash, and unlock the potential for streamlined searches in your sorted arrays!

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy