The following example shows how you can loop over an Array in ActionScript 3.0 using the Array class’s every() method.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/06/13/looping-over-an-array-using-the-every-method-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:Style>
.greenModal {
modalTransparencyColor: haloGreen;
}
.redModal {
modalTransparencyColor: red;
}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.utils.StringUtil;
private function checkArray(arr:Array):void {
textArea.text = "";
dataGrid.dataProvider = arr;
var success:Boolean = arr.every(isNumeric);
if (success) {
Application.application.styleName = "greenModal";
Alert.show("Array is numeric.", // text
"SUCCESS", // title
Alert.OK, // flags
null, // parent
null); // closeHandler
} else {
Application.application.styleName = "redModal";
Alert.show("Array has non-numeric elements.",
"ERROR",
Alert.OK,
null,
null);
}
}
private function isNumeric(element:Object, index:int, arr:Array):Boolean {
var str:String = StringUtil.substitute("{0} ({1})\n",
element.label,
element.data);
textArea.text += str;
return ((element.hasOwnProperty("data")) &&
(element.data is Number));
}
]]>
</mx:Script>
<mx:Array id="numericArray1">
<mx:Object label="One" data="1" />
<mx:Object label="Two" data="2" />
<mx:Object label="Three" data="3" />
<mx:Object label="Four" />
<mx:Object label="Five" data="5" />
<mx:Object label="Six" data="6" />
<mx:Object label="Seven" data="7" />
</mx:Array>
<mx:Array id="numericArray2">
<mx:Object label="Eight" data="8" />
<mx:Object label="Nine" data="9" />
<mx:Object label="Ten" data="10" />
<mx:Object label="Eleven" data="11" />
<mx:Object label="Twelve" data="12" />
</mx:Array>
<mx:ApplicationControlBar dock="true">
<mx:Button label="Check Array 1"
click="checkArray(numericArray1);" />
<mx:Button label="Check Array 2"
click="checkArray(numericArray2);" />
</mx:ApplicationControlBar>
<mx:HBox>
<mx:DataGrid id="dataGrid" rowCount="7">
<mx:columns>
<mx:DataGridColumn dataField="label" />
<mx:DataGridColumn dataField="data" />
</mx:columns>
</mx:DataGrid>
<mx:TextArea id="textArea"
editable="false"
width="{dataGrid.width}"
height="{dataGrid.height}" />
</mx:HBox>
</mx:Application>
View source is enabled in the following example.
Here is a slightly less complex example:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/06/13/looping-over-an-array-using-the-every-method-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white"
creationComplete="init();">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.utils.StringUtil;
private var arr:Array;
private function init():void {
arr = [];
arr.push({label:"One", data:1});
arr.push({label:"Two", data:2});
arr.push({label:"Three", data:3});
arr.push({label:"Four"});
arr.push({label:"Five", data:5});
arr.push({label:"Six", data:6});
arr.push({label:"Seven", data:7});
checkArray(arr);
}
private function checkArray(arr:Array):void {
textArea.text = "";
if (arr.every(isNumeric)) {
Alert.show("Array is numeric.");
} else {
Alert.show("Array has non-numeric elements.");
}
}
private function isNumeric(element:Object, index:int, arr:Array):Boolean {
var str:String = StringUtil.substitute("{0} ({1})\n",
element.label,
element.data);
textArea.text += str;
return ((element.hasOwnProperty("data")) &&
(element.data is Number));
}
]]>
</mx:Script>
<mx:TextArea id="textArea"
editable="false"
width="160"
height="120" />
</mx:Application>





The title of this blog needs a correction - every() is Array’s method not Alert’s one.
this site is nice and very helpful.
here is an another nice site http://web-adobe.blogspot.com
yeah i agree, thanks for the example but it seems way too bloated… you didn’t have to style your Alert and have all that extra malarky… You could have given a much shorter example. After all it’s just one method you have to demonstrate…
Yakov Fain,
Thanks for the heads up. I changed the description.
Peter
ryan,
Thanks for the advice. I added a shorter/simpler example.
Peter