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);
}
}
How remove items from ArrayCollection in flex?
-
05-08-2022 - |
Question
This is my ArrayCollection I want to remove "Label:"Taxes", Value:2000" line from this arrayCollection. Guys how can do that without using forloops or using 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}
]);
Solution 2
OTHER TIPS
For doing that you need to know item index.
So expenses.removeItemAt(0)
will work for you.
If you have object but don't know index you can get it with expenses.getItemIndex(myObject)
and then remove it.
//Removing object using "remove" method as following:
trace("Remove First Item: "+ expenses.removeItemAt(0));
//Removing object using loop as following:
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);
}
}
Hope this may help you.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow