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.

View MXML

<?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>
 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

0 Responses to Looping over an Array using the every() method in Flex

  1. Yakov Fain says:

    The title of this blog needs a correction – every() is Array’s method not Alert’s one.

  2. cfsuman says:

    this site is nice and very helpful.
    here is an another nice site http://web-adobe.blogspot.com

  3. ryan says:

    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…

  4. peterd says:

    Yakov Fain,

    Thanks for the heads up. I changed the description.

    Peter

  5. peterd says:

    ryan,

    Thanks for the advice. I added a shorter/simpler example.

    Peter

  6. Puneet Mishra says:

    Sir i am having problem to catch a value in the pop up can any on help

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree