Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > Flex > Flex 2, 3 & 4

Reply
 
Thread Tools Rate Thread Display Modes
Old 10-11-2007, 03:11 PM   #1
blu3
Senior Member
 
blu3's Avatar
 
Join Date: Jun 2006
Location: Croatia
Posts: 210
Default Removing item from ArrayCollection

If i have an ArrayCollection like this one:
Code:
public var myAC:ArrayCollection = new ArrayCollection([{name: 'First, price: '10'}]);
myAC.addItem({name:"Second", price:"20"});
How can i for example remove {name:"First" price:"10"} item from ArrayCollection?

thanks in advance
__________________
BLUE
blu3 is offline   Reply With Quote
Old 10-11-2007, 04:58 PM   #2
Jim Freer
Senior Member
 
Join Date: Jul 2007
Location: Houston, TX
Posts: 355
Send a message via Yahoo to Jim Freer
Default

ArrayCollection is based on ListCollectionView which has a function removeItemAt().

So: myAC.removeItemAt(0);

Jim
http://freerpad.blogspot.com/
Jim Freer is offline   Reply With Quote
Old 10-11-2007, 05:39 PM   #3
dr_zeus
Feathers
 
dr_zeus's Avatar
 
Join Date: Mar 2005
Location: Silicon Valley
Posts: 1,999
Default

Combine it with getItemIndex() and you should be able to do it.

Please note, however, that you probably need to reference the exact object that you're removing. I'm guessing that the following code will never work:

ActionScript Code:
collection.getItemIndex({name:"First" price:"10"});

Equality checks on the Object type is by reference.
__________________
Josh Tynjala | Feathers
dr_zeus is offline   Reply With Quote
Old 10-12-2007, 01:24 PM   #4
blu3
Senior Member
 
blu3's Avatar
 
Join Date: Jun 2006
Location: Croatia
Posts: 210
Default

Quote:
Originally Posted by dr_zeus View Post
Combine it with getItemIndex() and you should be able to do it.

Please note, however, that you probably need to reference the exact object that you're removing. I'm guessing that the following code will never work:

ActionScript Code:
collection.getItemIndex({name:"First" price:"10"});

Equality checks on the Object type is by reference.
You are guessing right dr_zeus, the above code won't work. I can remove object using myAC.removeItemAt(0) if I know it's index but:

Code:
myAC.removeItemAt(myAC.getItemIndex({name:"First" price:"10"}));
throws an error.
__________________
BLUE
blu3 is offline   Reply With Quote
Old 10-12-2007, 05:06 PM   #5
dr_zeus
Feathers
 
dr_zeus's Avatar
 
Join Date: Mar 2005
Location: Silicon Valley
Posts: 1,999
Default

Right, as I said, you need a reference to the actual item you're removing. Objects are equality checked by reference rather than value.
__________________
Josh Tynjala | Feathers
dr_zeus is offline   Reply With Quote
Old 10-12-2007, 08:32 PM   #6
blu3
Senior Member
 
blu3's Avatar
 
Join Date: Jun 2006
Location: Croatia
Posts: 210
Default

And how can i reference the object i am going to remove? I mean, i have an ArrayCollection filled with data and i want to remove an item with specific values just like in my example above. How can i reference it if getItemIndex method doesn't work?
__________________
BLUE
blu3 is offline   Reply With Quote
Old 10-12-2007, 08:50 PM   #7
dr_zeus
Feathers
 
dr_zeus's Avatar
 
Join Date: Mar 2005
Location: Silicon Valley
Posts: 1,999
Default

Actually, it's not too hard. The ArrayCollection exposes the regular Array it wraps through its "source" property. Array has a filter() method that lets you run a function on every item where you can check for any conditions that you wish. The Array returned by filter() will have only the item reference(s) you need.
__________________
Josh Tynjala | Feathers
dr_zeus is offline   Reply With Quote
Old 10-13-2007, 09:00 AM   #8
blu3
Senior Member
 
blu3's Avatar
 
Join Date: Jun 2006
Location: Croatia
Posts: 210
Default

You helped me a lot dr_zeus, thank you very much. I really appreciate it.
__________________
BLUE
blu3 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding additoinal fields to existing item in ArrayCollection nprzybylo Flex 2, 3 & 4 2 02-16-2007 03:40 PM
dataGrid - removing an item mojito Components 1 02-07-2007 11:16 AM
removing a specific item from an array ubergrafik ActionScript 2.0 4 01-29-2006 11:15 PM
problem removing item from an array lisaad ActionScript 2.0 2 04-29-2005 01:22 AM
removing a specified item from an array? Clox ActionScript 2.0 2 11-16-2004 05:11 AM


All times are GMT. The time now is 12:38 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.