How to use Array.prototype.reduceRight() Method In Javascript

The Array.prototype.reduceRight() method applies a function against an accumulator and each value of the array (from right to left) to reduce it to a single value. We can use this method to find the last occurrence of a substring by iterating the string array in reverse.

Example: In this example, we use reduceRight() to find the last occurrence index of the substring “example” in the given text. The method accumulates the index if the substring is found, otherwise returns -1.

JavaScript
const text = "This is an example text with example as a repeated example.";

// Substring to search for
const substring = "example";

// Using reduceRight() method
const lastIndex = text.split('').reduceRight((acc, _, i, arr) => {
    if (arr.slice(i, i + substring.length).join('') === substring && acc === -1) {
        return i;
    }
    return acc;
}, -1);

if (lastIndex !== -1) {
    console.log(`Last occurrence of '${substring}' found at index ${lastIndex}`);
} else {
    console.log(`'${substring}' not found in the text.`);
}

Output
Last occurrence of 'example' found at index 51




JavaScript Program to Find the Index of the Last Occurrence of a Substring in a String

Finding the index of the last occurrence of a substring in a string is a common task in JavaScript. The last occurrence of a substring in a string in JavaScript refers to finding the final position where a specified substring appears within a given string. We may want to know the position of the last occurrence of a specific substring within a given string.

There are several methods that can be used to find the index of the last occurrence of a substring in a string in JavaScript, which are listed below:

Table of Content

  • Using lastIndexOf() Method
  • Using Regular Expression
  • Using split() and pop()
  • Using a Loop
  • Using Array.prototype.reduceRight() Method

We will explore all the above methods along with their basic implementation with the help of examples.

Similar Reads

Using lastIndexOf() Method

The lastIndexOf() method in JavaScript is used to find the index of the last occurrence of a specified substring in a string. It searches the string from the end to the beginning and returns the index of the last occurrence of the substring, or -1 if the substring is not found....

Using Regular Expression

Regular expressions provide a powerful way to search for patterns in strings. You can use the RegExp constructor along with the exec() method to find the last occurrence of a substring in a string....

Using split() and pop()

Using split() and pop(), this approach splits the text at the substring, extracts the last segment, and calculates the index of its start within the original string....

Using a Loop

Using a loop, iterate through the string, comparing substrings of the same length as the target substring with it. Track the index of the last occurrence. Return the index found after iterating through the entire string....

Using Array.prototype.reduceRight() Method

The Array.prototype.reduceRight() method applies a function against an accumulator and each value of the array (from right to left) to reduce it to a single value. We can use this method to find the last occurrence of a substring by iterating the string array in reverse....