- Home /
FindIndex in a List
I need to get an index of an object in List with a certain condition.
class Marker { public var index : int; }
var List.<Marker> allMarkers;
public function GetIndexForMarker (_marker : Marker) : int { return allMarkers.FindIndex(RoadMarker m => m.markerIndex == _marker.markerIndex); //This doesn't work. }
What basically I need to implement is the following, but with FindIndex to make it shorter.
public function GetIndexForMarker (_marker : Marker) : int { var i : int = 0; for (var currentMarker : Marker in allMarkers) { if (currentMarker.index == _marker.index) return i;
i++;
} }
How can I use FindIndex in JavaScript?
Answer by Bunny83 · Apr 13, 2011 at 10:35 PM
The index you refer to is a variable of your Marker class. So the best way to search a specific number is that you're using in your second example. I don't even think the first one is javascript....
Anyways, if your the index value of your Marker is the same as the index into the List you can just use the IndexOf() method...
int index = allMarkers.IndexOf(marker);
edit
Since i don't have much experience with LINQ and lambda expressions i can't say much about that, but i guess i've found what you're after. According to this question you should look here:
http://unity3d.com/support/documentation/Manual/MonoUpgradeDetails.html
Your first code looks like a mix of javascript and a bit LINQ to me... LINQ would be another way to do what you want, but i'm not familiar with LINQ ;)
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Java Script command list 3 Answers
How to make a List Constructor? 1 Answer
Make an array containting multiple classes 1 Answer
Can you define a jagged Generic List in UnityScript? 1 Answer