Archive

Posts Tagged ‘arraycollection’

Extracting unique values from an ArrayCollection

September 4th, 2007 2 comments

Situation: let’s say that we have an ArrayCollection which keeps informations about products. We need an Array of unique values extracted from the ArrayCollection, for instance to fill up a ComboBox with measure units of the products.

Fancy solution:

allProducts is our ArrayCollection of products (which was previously declared and filled with informations). uniMeasures is the Array of measure units, which will be filled from allProducts. The property in which we are interested in is mu (measure unit).

Code:

1
2
3
4
5
6
7
8
9
private function makeUnitz(element: Object, index: int, arr:Array):void{
 
if (uniMeasures.length != 0) { //the most probable test first, for optimization
 
if (uniMeasures.indexOf(element.mu)==-1) uniMeasures.push(element.mu);
 
}else uniMeasures.push(element.mu); //avoiding problems
 
}

Now, on the initialization of application (or ComboBox which has uniMeasures as dataProvider) we’ll say : allProducts.source.forEach(makeUnitz); which will get the job done, fancy way.