这是我的ArrayCollection我想从这个arrayCollection中删除“Label:”Taxes“,Value:2000”行。伙计们如何在不使用forloops或使用forloop的情况下做到这一点?
public var expenses:ArrayCollection = new ArrayCollection([
{Label:"Taxes", Value:2000},
{Label:"Rent", Value:1000},
{Label:"Bills", Value:100},
{Label:"Car", Value:450},
{Label:"Gas", Value:100},
{Label:"Food", Value:200}
]);
答案 0 :(得分:2)
为此,您需要知道项目索引。
所以expenses.removeItemAt(0)
会对你有用。
如果您有对象但不知道索引,则可以使用expenses.getItemIndex(myObject)
获取它,然后将其删除。
答案 1 :(得分:1)
public var expenses:ArrayCollection = new ArrayCollection([
{Label:"Taxes", Value:2000},
{Label:"Rent", Value:1000},
{Label:"Bills", Value:100},
{Label:"Car", Value:450},
{Label:"Gas", Value:100},
{Label:"Food", Value:200}
]);
var tempArr:ArrayCollection = new ArrayCollection(); // temporary ArrayCollection
tempArr = ObjectUtil.copy(expenses) as ArrayCollection; //copy expences ArrayCollecttion to temp arrray before delete items
for(var i:int=0;i<expenses.length;i++)
{
if(expenses[i].Label.toString()==label)
{
tempArr.removeItemAt(i);
}
}
答案 2 :(得分:0)
//使用&#34删除对象;删除&#34;方法如下:
trace("Remove First Item: "+ expenses.removeItemAt(0));
//使用循环删除对象如下:
var indetToDelete:Object = expenses[expenses.length - expenses.length];
for(var item:int=0; item<expenses.length ; item++){
trace("Label:"+expenses[item].Label+"Value:"+expenses[item].Value);
if((expenses[item].Label == indetToDelete.Label) && (expenses[item].Value == indetToDelete.Value)){
expenses.removeItemAt(item);
}
}
希望这可以帮助你。