0

Flash Builder 4, AS3.

In an ArrayCollection need to count the records in a field, for example need to know how many users have age of 25. This is the ArrayCollection dataProvider for my DataGrid.

In SQL is easy, just need the command: select age, count (age) from employees where age = '25';

But ArrayCollection tried several ways and could not, could someone help me?

Thank you very much!

user2530802
  • 119
  • 2
  • 10

1 Answers1

3

This should work assuming the ArrayCollection has objects with a field age:

var ac:ArrayCollection = new ArrayCollection([ { "name":"test1", "age":25 }, { "name":"test1", "age":20 }, { "name":"test1", "age":25 } ]);
var count:int = 0;
for each (var item:Object in ac) 
{
    if (item.age == 25)
        ++count;
}
trace(count);
Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66
  • +1; This is the most direct approach. You could also apply a filter to the ArrayCollection and then use the length after the ArrayCollection is applied. That would also have display implications on any components using the collection as a dataProvider, though. – JeffryHouser Jul 02 '13 at 01:08