Adobe Flex / Actionscript - Min Max of Array Collection
I have an ArrayCollection derived from a httpService call where the XML looks like the following:
<data> <label>John</label><height>5.5</height> <label>John</label><height>7.2</height> <label>John</label>开发者_JS百科<height>3.2</height> </data>
I know how to use Math.min and Math.max on an array but how would I get the min and max of just the height in this example? Thanks!
I would just use the ArrayCollection
's Sort
method to sort it by height. Then just take the height values from the first and last records in the ArrayCollection
.
Update:
I'll add shaunhusain's solution since code formatting in comments isn't the best.
public var minValue:int = int.MAX_VALUE;
public var maxValue:int = -1;
for each (var o:Object in myArrayCollection)
{
if (o.height > maxValue)
maxValue = o.height;
if (o.height < minValue)
minValue = o.height;
}
Do you already have data stored in the ArrayCollection? If so, use the Debugger to see the structure of the collection. After that it should be as simple as looping through the collection and finding the min and max. If data is like myAC[0]['height'],myAC[1]['height'] etc you can't use Math methods.
精彩评论