Horje
How to Find the Index of an Element that Contains the Given Substring in Array ?

To find the index of an element within an array that contains a specific substring in JavaScript, there are multiple strategies available. The methods offer flexibility in handling various scenarios, allowing you to efficiently locate the index of the desired substring within the array.

Using findIndex() with includes()

We can use the findIndex() method with the includes() method to directly search for the substring in the array and return the index of the same.

Syntax:

array.findIndex(element => element.includes(substring))

Example: The below code example finds the index of an element that contains the given substring in an array.

Javascript

const array =
    ['apple', 'banana', 'orange', 'grape'];
const substring = 'an';
 
const index =
    array.findIndex
    (element => element.includes(substring));
console.log(
    "The first appearence of the given substring " +
    "found in the element present at index", index);

Output

The first appearence of the given substring found in the element present at index 1

Using findIndex() with indexOf()

Use the findIndex() with indexOf() method to check if the substring exists in any of the element contained by the array.

Syntax:

array.findIndex(element => element.indexOf(substring) !== -1)

Example: The below example finds the index of an element that contains the given substring in array using indexOf().

Javascript

const array =
    ['apple', 'banana', 'orange', 'grape'];
const substring = 'rap';
 
const index =
    array.findIndex
    (element => element.indexOf(substring) !== -1);
console.log(
    "The first appearence of the given substring " +
    "found in the element present at index", index);

Output

The first appearence of the given substring found in the element present at index 3

Using a loop

Employ a for loop to iterate through the array and manually check each element for the presence of the substring using the string includes() method.

Syntax:

for (let i = 0; i < array.length; i++) {}

Example: The below example finds the index of an element that contains the given substring in array using for loop.

Javascript

function findIndexWithLoop(array, substring) {
    for (let i = 0; i < array.length; i++) {
        if (array[i].includes(substring)) {
            return i;
        }
    }
    return -1;
}
 
const array =
    ['apple', 'banana', 'orange', 'grape'];
const index =
    findIndexWithLoop(array, 'ran');
console.log(
    "The first appearence of the given substring " +
    "found in the element present at index", index);

Output

The first appearence of the given substring found in the element present at index 2



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
What are Ternary Operator in JavaScript? What are Ternary Operator in JavaScript?
Maximum Frequency Character in String in JavaScript Maximum Frequency Character in String in JavaScript
JavaScript Program to Calculate Power Using Recursion JavaScript Program to Calculate Power Using Recursion
Replace all Occurrences of a Substring in a String in JavaScript Replace all Occurrences of a Substring in a String in JavaScript
How to Declare Variables in TypeScript ? How to Declare Variables in TypeScript ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11