It takes three arguments: element The current element being processed in the array. Syntax arr.findIndex( callback( element] ))Ī function to execute on each value in the array until the function returns true, indicating that the satisfying element was found. See also the find() method, which returns the value of an array element, instead of its index. If you'd like to contribute to the interactive examples project, please clone and send us a pull request. In the next tutorial, we will discuss Array some() method with the help of examples.The source for this interactive example is stored in a GitHub repository. Hope that you will have understood how to find the index of the first matching element in an array based on specific test condition using findIndex() method. In this tutorial, you have learned array findIndex() method in JavaScript with the help of example programs. ![]() ![]() The general syntax of the findIndex() method in JavaScript is: // Arrow functionĪrr.findIndex((element) => ) => name = 'Belly') Let index = numbers.findIndex(isGreater) Ĭonsole.log("Index of first greater number than 2: " +index) Find the index of first greater number than 2. Callback function to check greater number than 2. ![]() Otherwise, it returns -1 if no matching is found.įor example, the following snippet code returns the index of the first number that’s greater than 2. In other words, find() method of array returns the index of first array element that satisfies provided testing function (i.e.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |