Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the index of a particular item in array

Tags:

arrays

c#

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?

like image 621
Mac Avatar asked Dec 08 '10 14:12

Mac


People also ask

What method can we use to find the index of a particular element in an array Codehs?

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.

How do you get the index of an element in a list in JS?

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.

How do you get a specific value from an array in JavaScript?

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() .


1 Answers

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?

like image 182
GvS Avatar answered Sep 24 '22 06:09

GvS