4 Replies Latest reply on Jan 13, 2010 4:30 AM by Francisc

    Remove ArrayCollection element

    Francisc Level 3

      Hello!

       

      Does: myAC[3]=null actually remove the element of the AC?

      And by that I mean completly suppressing the element with the key 3.

       

      For example if the AC had 5 elements: 0,1,2,3,4, myAC[3]=null does what to the AC?

      A) 0,1,2,4

      - or -

      B) 0,1,2,3, where 3 is the old 4?

       

       

      Thank you!

        • 1. Re: Remove ArrayCollection element
          Sebastien V. Level 3

          No it does not.

           

          This will just set the third element to null. So neither A or B will occur.

          The result will be 0,1,2,3,4, with myAC[3] == null.

           

          Removing an element from an ArrayCollection should be done using the removeItemAt method.

          • 2. Re: Remove ArrayCollection element
            Matt Le Fevre Level 4

            myAC[3]=null

             

            would make the value at index 3 equal to null, it wouldn't create any shift in item:index values.

             

            myAC.removeItemAt(3)

             

            would cause the item at index 3 to be removed completely, [0][1][2] would remain stationary, and all the indexs above [3] would be moved along one to fill the gap.

             

            eg:

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="doThis()">

                <mx:ArrayCollection id="arrCol">
                    <mx:source>
                        <mx:Array>
                            <mx:Object label="Student A" score="85" />
                            <mx:Object label="Student B" score="48" />
                            <mx:Object label="Student C" score="71" />
                            <mx:Object label="Student D" score="88" />
                            <mx:Object label="Student E" score="24" />
                            <mx:Object label="Student F" score="64" />
                            <mx:Object label="Student G" score="76" />
                            <mx:Object label="Student H" score="76" />
                            <mx:Object label="Student I" score="93" />
                            <mx:Object label="Student J" score="88" />
                            <mx:Object label="Student K" score="48" />
                            <mx:Object label="Student L" score="76" />
                        </mx:Array>
                    </mx:source>
                </mx:ArrayCollection>

                <mx:Script>
                    <![CDATA[
                        import mx.collections.ArrayCollection;
                       
                        public function doThis():void
                        {
                            trace(arrCol.getItemAt(1).label);
                            arrCol.removeItemAt(1);
                            trace(arrCol.getItemAt(1).label);
                        }
                       
                       
                    ]]>
                </mx:Script>
            </mx:Application>

             

            would give a trace of

             

            Student B

            Student C

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="doThis()">

                <mx:ArrayCollection id="arrCol">
                    <mx:source>
                        <mx:Array>
                            <mx:Object label="Student A" score="85" />
                            <mx:Object label="Student B" score="48" />
                            <mx:Object label="Student C" score="71" />
                            <mx:Object label="Student D" score="88" />
                            <mx:Object label="Student E" score="24" />
                            <mx:Object label="Student F" score="64" />
                            <mx:Object label="Student G" score="76" />
                            <mx:Object label="Student H" score="76" />
                            <mx:Object label="Student I" score="93" />
                            <mx:Object label="Student J" score="88" />
                            <mx:Object label="Student K" score="48" />
                            <mx:Object label="Student L" score="76" />
                        </mx:Array>
                    </mx:source>
                </mx:ArrayCollection>

                <mx:Script>
                    <![CDATA[
                        import mx.collections.ArrayCollection;
                       
                        public function doThis():void
                        {
                            trace(arrCol.getItemAt(1).label);
                            arrCol[1] = null;
                            trace(arrCol.getItemAt(1).label);
                        }
                       
                       
                    ]]>
                </mx:Script>
            </mx:Application>

             

            would give you a trace of

             

            Student B

            Error #1009; cannot access a null object reference

            1 person found this helpful
            • 3. Re: Remove ArrayCollection element
              archemedia Level 4

              The above examples are good for ArrayCollections. If you have an array, use the splice method:

               

              var arr:Array = [1, 2, 3, 4];      //[1, 2, 3, 4]
              arr.splice(2, 1);                       //[1, 2, 4]

               

              If you have an ArrayCollection, you should indeed use removeItemAt(pos)

              You could also use the splice method on the source array off course:

               

              myAC.source.splice(2,1);

               

               

              d

              1 person found this helpful
              • 4. Re: Remove ArrayCollection element
                Francisc Level 3

                Thank you!