How to Find the Index of an Element that Contains the Given Substring in Array ? Last Updated : 20 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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. Table of Content Using findIndex() with includes()Using findIndex() with indexOf()Using a loopUsing 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); OutputThe 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); OutputThe first appearence of the given substring found in the element present at index 3 Using a loopEmploy 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); OutputThe first appearence of the given substring found in the element present at index 2 Comment More infoAdvertise with us Next Article How to Find the Index of an Element that Contains the Given Substring in Array ? A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to find the index of an element in an array using PHP ? In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approachesTable of ContentUsing array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing ar 6 min read How to Find Index of Element in Array in MATLAB? In MATLAB, the arrays are used to represent the information and data. You can use indexing to access the elements of the array. Â In MATLAB the array indexing starts from 1. To find the index of the element in the array, you can use the find() function. Using the find() function you can find the indi 4 min read How to find if an array contains a specific string in JavaScript/jQuery ? In order to Check if an array contains a specific string be done in many ways in Javascript. One of the most common methods to do this is by using the traditional for loop. In Javascript, we have several other modern approaches which help in finding a specific string in an array to iterate the array 5 min read JavaScript - How To Check Whether a String Contains a Substring? Here are the different methods to check whether a string contains a substring in JavaScript.1. Using includes() MethodThe includes() method is the most simple and modern way to check if a string contains a specific substring. It returns true if the substring is found within the string, and false oth 3 min read Find and return index of given String in a Multidimensional Array Given a multidimensional array stringArr of strings and a keyString to be found, the task is to return the coordinates/indexes of that key string in the array. Example: Input: stringArr[][] = { { "a", "h", "b"}, {"c", "d", "e"}, {"g", "t", "r"} }, keyString = "e"Output = {1, 2}Explanation: Following 10 min read Check If An Array of Strings Contains a Substring in JavaScript Here are the different ways to check if an array of strings contains a substring in JavaScript1. Using includes() and filter() methodWe will create an array of strings, then use includes() and filter() methods to check whether the array elements contain a sub-string.JavaScriptconst a = ['Geeks', 'gf 4 min read Difference Between indexOf and findIndex function of array The task is to differentiate between the indexOf() and findIndex() methods of JavaScript. we're going to discuss both approaches. JavaScript indexOf() Method: This method is used to find the index of the first occurrence of the elements provided for search as the argument to the function. Syntax: ar 3 min read Find Index of Cells Containing My String in MATLAB Cell Arrays in MATLAB are a type of array that store data in the form of cells. The benefit of using these cell arrays is that they can store data of different types as cells within a cell array only. In this article, we will see how to find a given string in a cell array. Firstly, we shall check wh 2 min read Shell Program to Find the Position of Substring in Given String A string is made of many substrings or can say that if we delete one or more characters from the beginning or end then the remaining string is called substring. This article is about to write a shell program that will tell the position (index) of the substring in a given string. Let's take an exampl 7 min read Like