0

I am attempting to build an array of arrays that will with the following syntax. My output is definitely not what I am trying to achieve. Here is my code:

var labs:ArrayCollection = new ArrayCollection();
    var sets:ArrayCollection = new ArrayCollection();

    var labsArray:Array = ["ProDPI","WHCC","Tin"];
    var setsArray:Array = ["Set1","Set2","Set3"];

    var folders:ArrayCollection = new ArrayCollection();
    var foldersArray:Array = [labsArray, setsArray];

    var objFolderBuild:Object = new Object;

        for (var i:int = 0; i< foldersArray.length;i++) {
                for (var j:int = 0; j < foldersArray[i].length; j++) {
                    objFolderBuild.labName = foldersArray[i][j];
                    folders.addItem(objFolderBuild);
                }
        }
labFolderList.labelField="labName";
labFolderList.dataProvider=folders;
setFolderList.labelField="setName";
setFolderList.dataProvider=folders;

My output is coming out as 6 references to "Set3". There is obviously something wrong with my for loops, but I can't figure it out. Any ideas would be a great help.

2
  • Am I missing the point where you actually output something in the code? What output are you referring to? Commented Mar 11, 2012 at 19:11
  • the labFolderList.labelField = "labName" <= Using this to change the labelField to populate a ComboBox Commented Mar 11, 2012 at 19:13

1 Answer 1

2

I threw together this quick app to prove that your code is properly tracing out the foldersArray[i][j]. It's 90% the code from your app.

However, I want to point out that the objFoldersBuild object is only created once. So what you do each time through the loop is change the value of an existing object's property and add it to your folders ArrayCollection. You have an ArrayCollection with multiple copies of the same exact object. The answer is to create a new object each time through the loop.

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx" preinitialize="windowedapplication1_preinitializeHandler(event)">
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.events.FlexEvent;

            protected function windowedapplication1_preinitializeHandler(event:FlexEvent):void
            {
                var labs:ArrayCollection = new ArrayCollection();
                var sets:ArrayCollection = new ArrayCollection();

                var labsArray:Array = ["ProDPI","WHCC","Tin"];
                var setsArray:Array = ["Set1","Set2","Set3"];

                var folders:ArrayCollection = new ArrayCollection();
                var foldersArray:Array = [labsArray, setsArray];

                var objFolderBuild:Object = new Object;

                for (var i:int = 0; i< foldersArray.length;i++) {
                    for (var j:int = 0; j < foldersArray[i].length; j++) {
                        trace(foldersArray[i][j]);
                                objFolderBuild  = new Object;
                        objFolderBuild.labName = foldersArray[i][j];
                        folders.addItem(objFolderBuild);
                    }
                }
/*              labFolderList.labelField="labName";
                labFolderList.dataProvider=folders;
                setFolderList.labelField="setName";
                setFolderList.dataProvider=folders; */
            }

        ]]>
    </fx:Script>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
</s:WindowedApplication>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.