2

How do I add an item to a multidimensional array? I want to add the price to item2 but I get "push is not a function"

<fx:Script>
    <![CDATA[

        [Bindable]
        public var dp:Array = [ 
            { label: "item1", desc: "this is item 1"  },
            { label: "item2", desc: "this is item 2"  },
            { label: "item3", desc: "this is item 3"  }
        ];


        private function addItem():void{
            var v:String = '$8.99';
            dp[1].push( ("price:", v ) ); 
            dp.refresh();

        }
    ]]>
</fx:Script>


<mx:VBox> 
    <mx:Repeater id="r" dataProvider="{myAC}">
        <mx:RadioButton label="{r.currentItem.label}"/>
        <mx:Text text="{r.currentItem.desc}"/>
        <mx:Text text="{r.currentItem.price}"/>
    </mx:Repeater>


<s:Button label="push" click="addItem()"/>


</mx:VBox> 


</mx:Application>

1 Answer 1

4

You have an array of objects, not an array of arrays. Try this:

    [Bindable]
    public var dp:Array = [ 
        { label: "item1", desc: "this is item 1", price : ""  },
        { label: "item2", desc: "this is item 2", price : ""  },
        { label: "item3", desc: "this is item 3", price : ""  }
    ];

    private function addItem():void{
        var v:String = '$8.99';
        dp[1]["price"] = v; 
        dp.refresh();

    }
Sign up to request clarification or add additional context in comments.

1 Comment

use ArrayCollection instead of Array: public var dp:ArrayCollection = new ArrayCollection([ { label: "item1", desc: "this is item 1", price : "" }, { label: "item2", desc: "this is item 2", price : "" }, { label: "item3", desc: "this is item 3", price : "" } ]); see: blog.flexdevelopers.com/2009/03/…

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.