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

by Peter deHaan on June 13, 2008

in ActionScript, Alert, Array

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>

{ 6 comments… read them below or add one }

1 Yakov Fain June 14, 2008 at 3:46 am

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

Reply

2 cfsuman June 14, 2008 at 4:45 am

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

Reply

3 ryan June 14, 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…

Reply

4 peterd June 14, 2008 at 10:01 pm

Yakov Fain,

Thanks for the heads up. I changed the description.

Peter

Reply

5 peterd June 14, 2008 at 10:02 pm

ryan,

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

Peter

Reply

6 Puneet Mishra February 17, 2009 at 6:39 am

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

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: