AS3: Array.filter R0xr! December 16, 2007
Array in AS3 has some nifty features that make managing and manipulating active data much easier. Thanks to Brandon Hall's nooks and crannies presentation at LA Flash for getting me to look. Here's an example of using Array.filter
Here's the minimum filter that doesn't do anything. I default the last parameters as often filters are useful outside of iterating over arrays, but the filter method nicely tells the filter where it is via the 'index' and a reference back to the Array.
-
-
public class Filter {
-
public function passesFilter(item:*, index:int= 0, array:Array = null):Boolean{
-
var passes:Boolean = true;
-
return passes;
-
}
-
}
Inside com.troyworks.data.filters. I've created a family of filters, in particular RangeFilter that allows inclusive and exclusive of a given range. Here's what a 3-8 filter looks like:
3-8 filter.. 0123456789012345678
* inclusive ---[PASS]----------
noninclusive ---]PASS[----------
The real power of filters is they can be stacked. Here's an example of using a color filter, that looks for color values in the midgray band, but adds an additional filter so that values to close to gray are thrown out.
-
-
//find values from 0x555555 to 0xBBBBBB non-inclusive on both sides
-
var midsFilter:ColorStatisticFilter = new ColorStatisticFilter( ColorUtil.GRAY5, ColorUtil.GRAYB, false, false);
-
midsFilter.distanceFromShadeFilter = new NumberRangeBooleanFilter(36, 255);
-
// second param needs to be null as this filter is a function closure instead of an anonymous function.
-
var midColors:Array = indexedColors.filter(midsFilter.passesFilter,null);

Add a Comment: