简体   繁体   English

Adobe Flex / Actionscript - 最小阵列集合

[英]Adobe Flex / Actionscript - Min Max of Array Collection

I have an ArrayCollection derived from a httpService call where the XML looks like the following: 我有一个从httpService调用派生的ArrayCollection,其中XML如下所示:

 <data> <label>John</label><height>5.5</height> <label>John</label><height>7.2</height> <label>John</label><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? 我知道如何在数组上使用Math.min和Math.max但是在这个例子中我如何获得高度的最小值和最大值? Thanks! 谢谢!

Do you already have data stored in the ArrayCollection? 您是否已将数据存储在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. 如果数据类似于myAC [0] ['height'],myAC [1] ['height']等,则不能使用数学方法。

I would just use the ArrayCollection 's Sort method to sort it by height. 我只想使用ArrayCollectionSort方法按高度对其进行排序。 Then just take the height values from the first and last records in the ArrayCollection . 然后从ArrayCollection的第一个和最后一个记录中获取高度值。

Update: 更新:

I'll add shaunhusain's solution since code formatting in comments isn't the best. 我将添加shaunhusain的解决方案,因为评论中的代码格式不是最好的。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM