<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Flex Examples &#187; Array</title>
	<atom:link href="http://blog.flexexamples.com/category/array/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.flexexamples.com</link>
	<description>Just a bunch of Adobe Flex Examples</description>
	<lastBuildDate>Wed, 26 Jan 2011 18:09:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Looping over an Array using the every() method in Flex</title>
		<link>http://blog.flexexamples.com/2008/06/13/looping-over-an-array-using-the-every-method-in-flex/</link>
		<comments>http://blog.flexexamples.com/2008/06/13/looping-over-an-array-using-the-every-method-in-flex/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 06:31:56 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Alert]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[every()]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2008/06/13/looping-over-an-array-using-the-every-method-in-flex/</guid>
		<description><![CDATA[<p>The following example shows how you can loop over an Array in ActionScript 3.0 using the Array class&#8217;s every() method.</p> <p>Full code after the jump.</p> <p></p> <p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/Array_every_test/bin/srcview/source/main.mxml.html">View MXML</a></p> &#60;?xml version="1.0" encoding="utf-8"?&#62; &#60;!-- http://blog.flexexamples.com/2008/06/13/looping-over-an-array-using-the-every-method-in-flex/ --&#62; &#60;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white"&#62; &#60;mx:Style&#62; .greenModal { modalTransparencyColor: haloGreen; } .redModal { modalTransparencyColor: red; } &#60;/mx:Style&#62; &#60;mx:Script&#62; &#60;![CDATA[ [...]]]></description>
			<content:encoded><![CDATA[<p>The following example shows how you can loop over an Array in ActionScript 3.0 using the Array class&#8217;s <code>every()</code> method.</p>
<p>Full code after the jump.</p>
<p><span id="more-600"></span></p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/Array_every_test/bin/srcview/source/main.mxml.html">View MXML</a></p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2008/06/13/looping-over-an-array-using-the-every-method-in-flex/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"&gt;

    &lt;mx:Style&gt;
        .greenModal {
            modalTransparencyColor: haloGreen;
        }

        .redModal {
            modalTransparencyColor: red;
        }
    &lt;/mx:Style&gt;

    &lt;mx:Script&gt;
        &lt;![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")) &#038;&#038;
                            (element.data is Number));
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:Array id="numericArray1"&gt;
        &lt;mx:Object label="One" data="1" /&gt;
        &lt;mx:Object label="Two" data="2" /&gt;
        &lt;mx:Object label="Three" data="3" /&gt;
        &lt;mx:Object label="Four" /&gt;
        &lt;mx:Object label="Five" data="5" /&gt;
        &lt;mx:Object label="Six" data="6" /&gt;
        &lt;mx:Object label="Seven" data="7" /&gt;
    &lt;/mx:Array&gt;

    &lt;mx:Array id="numericArray2"&gt;
        &lt;mx:Object label="Eight" data="8" /&gt;
        &lt;mx:Object label="Nine" data="9" /&gt;
        &lt;mx:Object label="Ten" data="10" /&gt;
        &lt;mx:Object label="Eleven" data="11" /&gt;
        &lt;mx:Object label="Twelve" data="12" /&gt;
    &lt;/mx:Array&gt;

    &lt;mx:ApplicationControlBar dock="true"&gt;
        &lt;mx:Button label="Check Array 1"
                click="checkArray(numericArray1);" /&gt;
        &lt;mx:Button label="Check Array 2"
                click="checkArray(numericArray2);" /&gt;
    &lt;/mx:ApplicationControlBar&gt;

    &lt;mx:HBox&gt;
        &lt;mx:DataGrid id="dataGrid" rowCount="7"&gt;
            &lt;mx:columns&gt;
                &lt;mx:DataGridColumn dataField="label" /&gt;
                &lt;mx:DataGridColumn dataField="data" /&gt;
            &lt;/mx:columns&gt;
        &lt;/mx:DataGrid&gt;
        &lt;mx:TextArea id="textArea"
                editable="false"
                width="{dataGrid.width}"
                height="{dataGrid.height}" /&gt;
    &lt;/mx:HBox&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/Array_every_test/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/Array_every_test/bin/main.html" width="100%" height="300"></iframe></p>
<p class="new">Here is a slightly less complex example:</p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2008/06/13/looping-over-an-array-using-the-every-method-in-flex/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();"&gt;

    &lt;mx:Script&gt;
        &lt;![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")) &#038;&#038;
                            (element.data is Number));
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:TextArea id="textArea"
            editable="false"
            width="160"
            height="120" /&gt;

&lt;/mx:Application&gt;
</pre>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Looping over an Array using the every() method in Flex on FlexExamples.com',url: 'http://blog.flexexamples.com/2008/06/13/looping-over-an-array-using-the-every-method-in-flex/',contentID: 'post-600',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'every()',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2008/06/13/looping-over-an-array-using-the-every-method-in-flex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Looping over each item in an Array in ActionScript 3.0</title>
		<link>http://blog.flexexamples.com/2008/04/21/looping-over-each-item-in-an-array-in-actionscript-30/</link>
		<comments>http://blog.flexexamples.com/2008/04/21/looping-over-each-item-in-an-array-in-actionscript-30/#comments</comments>
		<pubDate>Tue, 22 Apr 2008 06:52:08 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[forEach()]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2008/04/21/looping-over-each-item-in-an-array-in-actionscript-30/</guid>
		<description><![CDATA[<p>The following example shows how you can loop over an Array object and modify items using the Array class&#8217;s forEach() method.</p> <p>Full code after the jump.</p> <p></p> <p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/Array_forEach_test/bin/srcview/source/main.mxml.html">View MXML</a></p> &#60;?xml version="1.0" encoding="utf-8"?&#62; &#60;!-- http://blog.flexexamples.com/2008/04/21/looping-over-each-item-in-an-array-in-actionscript-30/ --&#62; &#60;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white" initialize="init();"&#62; &#60;mx:Script&#62; &#60;![CDATA[ import mx.utils.ObjectUtil; private function init():void { beforeTextArea.text = ObjectUtil.toString(dp); dp.forEach(forEach_test); [...]]]></description>
			<content:encoded><![CDATA[<p>The following example shows how you can loop over an Array object and modify items using the Array class&#8217;s <code>forEach()</code> method.</p>
<p>Full code after the jump.</p>
<p><span id="more-603"></span></p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/Array_forEach_test/bin/srcview/source/main.mxml.html">View MXML</a></p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2008/04/21/looping-over-each-item-in-an-array-in-actionscript-30/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            import mx.utils.ObjectUtil;

            private function init():void {
                beforeTextArea.text = ObjectUtil.toString(dp);
                dp.forEach(forEach_test);
                dataGrid.dataProvider = dp;
                afterTextArea.text = ObjectUtil.toString(dp);
            }

            private function forEach_test(element:*, index:int, arr:Array):void {
                element.lbl = element.name + " (" + element.abbr + ")";
                element.idx = index;
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:Array id="dp"&gt;
        &lt;mx:Object name="Baltimore Orioles" abbr="BAL" /&gt;
        &lt;mx:Object name="Boston Red Sox" abbr="BOS" /&gt;
        &lt;mx:Object name="Chicago White Sox" abbr="CWS" /&gt;
        &lt;mx:Object name="Cleveland Indians" abbr="CLE" /&gt;
        &lt;mx:Object name="Detroit Tigers" abbr="DET" /&gt;
        &lt;mx:Object name="Kansas City Royals" abbr="KC" /&gt;
        &lt;mx:Object name="Los Angeles Angels of Anaheim" abbr="LAA" /&gt;
        &lt;mx:Object name="Minnesota Twins" abbr="MIN" /&gt;
        &lt;mx:Object name="New York Yankees" abbr="NYY" /&gt;
        &lt;mx:Object name="Oakland Athletics" abbr="OAK" /&gt;
        &lt;mx:Object name="Seattle Mariners" abbr="SEA" /&gt;
        &lt;mx:Object name="Tampa Bay Devil Rays" abbr="TB" /&gt;
        &lt;mx:Object name="Texas Rangers" abbr="TEX" /&gt;
        &lt;mx:Object name="Toronto Blue Jays" abbr="TOR" /&gt;
    &lt;/mx:Array&gt;

    &lt;mx:DataGrid id="dataGrid"
            verticalScrollPolicy="on"
            width="100%"&gt;
        &lt;mx:columns&gt;
            &lt;mx:DataGridColumn dataField="name" /&gt;
            &lt;mx:DataGridColumn dataField="abbr" width="80" /&gt;
            &lt;mx:DataGridColumn dataField="lbl" /&gt;
            &lt;mx:DataGridColumn dataField="idx" width="40" textAlign="right" /&gt;
        &lt;/mx:columns&gt;
    &lt;/mx:DataGrid&gt;

    &lt;mx:Form width="100%" height="100%"&gt;
        &lt;mx:FormItem label="Before:" width="100%" height="50%"&gt;
            &lt;mx:TextArea id="beforeTextArea"
                    editable="false"
                    wordWrap="false"
                    width="100%"
                    height="100%" /&gt;
        &lt;/mx:FormItem&gt;
        &lt;mx:FormItem label="After:" width="100%" height="50%"&gt;
            &lt;mx:TextArea id="afterTextArea"
                    editable="false"
                    wordWrap="false"
                    width="100%"
                    height="100%" /&gt;
        &lt;/mx:FormItem&gt;
    &lt;/mx:Form&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information"><a href="http://blog.flexexamples.com/wp-content/uploads/Array_forEach_test/bin/srcview/index.html">View source</a> is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/Array_forEach_test/bin/main.html" width="100%" height="450"></iframe></p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Looping over each item in an Array in ActionScript 3.0 on FlexExamples.com',url: 'http://blog.flexexamples.com/2008/04/21/looping-over-each-item-in-an-array-in-actionscript-30/',contentID: 'post-603',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'forEach()',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2008/04/21/looping-over-each-item-in-an-array-in-actionscript-30/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Removing duplicate items from an array using the Array.filter() method</title>
		<link>http://blog.flexexamples.com/2007/08/05/removing-duplicate-items-from-an-array-using-the-arrayfilter-method/</link>
		<comments>http://blog.flexexamples.com/2007/08/05/removing-duplicate-items-from-an-array-using-the-arrayfilter-method/#comments</comments>
		<pubDate>Sun, 05 Aug 2007 18:07:18 +0000</pubDate>
		<dc:creator>Peter deHaan</dc:creator>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[ArrayCollection]]></category>
		<category><![CDATA[filter()]]></category>
		<category><![CDATA[hasOwnProperty()]]></category>

		<guid isPermaLink="false">http://blog.flexexamples.com/2007/08/05/removing-duplicate-items-from-an-array-using-the-arrayfilter-method/</guid>
		<description><![CDATA[<p>I&#8217;m sure there is an easier/better way to do this (and I&#8217;ll update this post accordingly if I find one) but here is a little sample which takes an array of items and filters out the duplicates.</p> <p>Full code after the jump.</p> <p></p> <p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/ArrayCollection_deduplicate_test/main.mxml">View MXML</a></p> &#60;?xml version="1.0" encoding="utf-8"?&#62; &#60;!-- http://blog.flexexamples.com/2007/08/05/removing-duplicate-items-from-an-array-using-the-arrayfilter-method/ --&#62; &#60;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure there is an easier/better way to do this (and I&#8217;ll update this post accordingly if I find one) but here is a little sample which takes an array of items and filters out the duplicates.</p>
<p>Full code after the jump.</p>
<p><span id="more-57"></span></p>
<p class="download"><a href="http://blog.flexexamples.com/wp-content/uploads/ArrayCollection_deduplicate_test/main.mxml">View MXML</a></p>
<pre class="code">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;!-- http://blog.flexexamples.com/2007/08/05/removing-duplicate-items-from-an-array-using-the-arrayfilter-method/ --&gt;
&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init()"&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            private var keys:Object = {};

            /**
             * Called by the Application container's creationComplete
             * event handler. This method creates a new Array object
             * which will be used as a data provider as well as a
             * filtered view of that array which does not contain
             * duplicated items.
             */
            private function init():void {
                /* Create a dummy data source with some semi-random
                    data. */
                var arr:Array = [];
                arr.push({data:1, label:"one"});
                arr.push({data:1, label:"one"});
                arr.push({data:1, label:"one"});
                arr.push({data:1, label:"one"});
                arr.push({data:2, label:"two"});
                arr.push({data:2, label:"two"});
                arr.push({data:2, label:"two"});
                arr.push({data:1, label:"one"});
                arr.push({data:3, label:"three"});
                arr.push({data:3, label:"three"});

                /* Filter the original array and call the
                    removeDuplicates() function on each item
                    in the array. */
                var filteredArr:Array = arr.filter(removedDuplicates);

                arrColl.source = arr;
                dedupedArrColl.source = filteredArr;
            }

            /**
             * This method is used to filter an array so that no
             * duplicate items are created. It works by first
             * checking to see if a keys object already contains
             * a key equal to the current value of the item.data
             * value. If the key already exists, the  current item
             * will not be readded to the data provider. If the key
             * does not already exist, add the key to the keys
             * object and add this item to the data provider.
             */
            private function removedDuplicates(item:Object, idx:uint, arr:Array):Boolean {
                if (keys.hasOwnProperty(item.data)) {
                    /* If the keys Object already has this property,
                        return false and discard this item. */
                    return false;
                } else {
                    /* Else the keys Object does *NOT* already have
                        this key, so add this item to the new data
                        provider. */
                    keys[item.data] = item;
                    return true;
                }
            }
        ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:ArrayCollection id="arrColl" /&gt;
    &lt;mx:ArrayCollection id="dedupedArrColl" /&gt;

    &lt;mx:HBox&gt;
        &lt;mx:VBox&gt;
            &lt;mx:Label text="Original ({arrColl.length} items):" /&gt;
            &lt;mx:List dataProvider="{arrColl}" /&gt;
        &lt;/mx:VBox&gt;
        &lt;mx:VBox&gt;
            &lt;mx:Label text="Filtered ({dedupedArrColl.length} items):" /&gt;
            &lt;mx:List dataProvider="{dedupedArrColl}" /&gt;
        &lt;/mx:VBox&gt;
    &lt;/mx:HBox&gt;

&lt;/mx:Application&gt;
</pre>
<p class="information">View source is enabled in the following example.</p>
<p><iframe src="http://blog.flexexamples.com/wp-content/uploads/ArrayCollection_deduplicate_test/bin/main.html" width="100%" height="240"></iframe></p>
<div class="evernoteSiteMemory"><a href="javascript:" onclick="Evernote.doClip({title: 'Removing duplicate items from an array using the Array.filter() method on FlexExamples.com',url: 'http://blog.flexexamples.com/2007/08/05/removing-duplicate-items-from-an-array-using-the-arrayfilter-method/',contentID: 'post-57',code: 'Pete9667',suggestNotebook: 'FlexExamples',suggestTags: 'filter(),hasOwnProperty()',providerName: 'FlexExamples.com',styling: 'text' });return false" class="evernoteSiteMemoryLink"><img src="http://static.evernote.com/article-clipper-remember.png" class="evernoteSiteMemoryButton" />
				</a>				<div class="evernoteSiteMemoryClear">&nbsp;</div>
</div>]]></content:encoded>
			<wfw:commentRss>http://blog.flexexamples.com/2007/08/05/removing-duplicate-items-from-an-array-using-the-arrayfilter-method/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

