user_78361084
user_78361084

Reputation: 3928

adding item to an array

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>

Upvotes: 2

Views: 6602

Answers (1)

wajiw
wajiw

Reputation: 12269

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();

    }

Upvotes: 4

Related Questions