I’m sure others already know this little trick – but in case you didn’t…
The filter
method for Arrays in Actionscript 3.0 comes in handy if you want to (ahem) *filter* certain elements out of your array :
var tmpArray:Array = ["oranges", "apples","oranges", "pineapple", "carrots", "oranges"];
var fruits:Array = tmpArray.filter(function(e, i, a){
var isUnique:Boolean = (a.indexOf(e) == i);
return (isUnique);
}, this);
trace(fruits.toString());
Simple really. Basically you define your logic and associate it with the filter
method. The filter method then iterates over each item in the Array, executing the logic as it goes (from first to last) and if it returns true
, the iterated record will be included in a new Array. If false
, it will be excluded.