13
Jun
08

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

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>

5 Responses to “Looping over an Array using the every() method in Flex”


  1. 1 Yakov Fain Jun 14th, 2008 at 3:46 am

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

  2. 2 cfsuman Jun 14th, 2008 at 4:45 am

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

  3. 3 ryan Jun 14th, 2008 at 11:47 am

    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. 4 peterd Jun 14th, 2008 at 10:01 pm

    Yakov Fain,

    Thanks for the heads up. I changed the description.

    Peter

  5. 5 peterd Jun 14th, 2008 at 10:02 pm

    ryan,

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

    Peter

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".