Search code examples
xmlapache-flexarraycollection

ArrayCollection children returns null


I have an ArrayCollection with following structure (when viewed in debug mode):

[0]
- [0]
-- src
- [1]
-- src

src is the path to an image.

I need to get all of the src's out of the arraycollection. However because of the first (unnamed) node I can't take them in.

I've tried ArrayCollection[0].children and save the result in another ArrayCollection, however the new ArrayCollection has 2 Objects in it but no 'src'. There are just 2 null objects

firstArrayCollection is filled with the data as described above.

secondArrayCollection.addItem(firstArrayCollection[0].children);

when viewing the content of secondArrayCollection i see following structure (and data):

[0] null
[1] null

Solution

  • try

    var i:int = 0;
    var myNormalArray:Array = new Array();
    
    //loop through collection
    for each (var child:* in myArrayCollection)
    {
      //do what you want with the child
      myNormalArray[i++] = child;
    }
    

    This will get all of the objects out of the collection into a normal array where they can be refered by their index. If you look at an array collection it has no concept of length so the index cannot be a index it has to be a key.

    or just do this thinking about it (although i've never done it)

    var myArray:Array = myArrayCollection.toArray();
    

    hope this helps

    Jon

    //********************************************************

    Second Attempt !!!!!!

    var pathArray:Array = new Array();
    var i:int = 0;
    for each (var child:* in myArrayCollection)
    {
      for each (var pathObject:Object in child)
      {
        pathArray[i++] = pathObject.src;
      }
    }
    

    this should work, i haven't test it though