将项添加到数组中

时间:2010-12-12 17:15:50

标签: flex actionscript-3

如何将项目添加到多维数组?我想将价格添加到item2,但我得到“推送不是功能”

                        

<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 个答案:

答案 0 :(得分:4)

您有一个对象数组,而不是数组数组。试试这个:

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

    }