I want to retrieve the index of an array but I know only a part of the actual value in the array.
For example, I am storing an author name in the array dynamically say "author = 'xyz'".
Now I want to find the index of the array item containing it, since I don't know the value part.
How to do this?
The indexOf() method is helpful if you want to know the index of a specific element. This method returns the index of the specified index or a -1 if the element is not in the array.
The indexOf() method returns the first index (position) of a specified value. The indexOf() method returns -1 if the value is not found. The indexOf() method starts at a specified index and searches from left to right. By default the search starts at the first element and ends at the last.
JavaScript Demo: Array.find() If you need to find the index of a value, use Array.prototype.indexOf() . (It's similar to findIndex() , but checks each element for equality with the value instead of using a testing function.) If you need to find if a value exists in an array, use Array.prototype.includes() .
You can use FindIndex
var index = Array.FindIndex(myArray, row => row.Author == "xyz");
Edit: I see you have an array of string, you can use any code to match, here an example with a simple contains:
var index = Array.FindIndex(myArray, row => row.Contains("Author='xyz'"));
Maybe you need to match using a regular expression?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With