4

When using .contains() on an ArrayCollection in Flex, it will always look at the memory reference. It does not appear to look at an .equals() method or .toString() method or anything overridable. Instead, I need to loop through the ArrayCollection every time and check each individual item until I find what I'm looking for.

Does anyone know why Flex/ActionScript was made this way? Why not provide a way from people to use the contains() method the way they want?

Richard Ev's user avatar

Richard Ev

54.4k61 gold badges196 silver badges283 bronze badges

asked Dec 23, 2008 at 19:36

DyreSchlock's user avatar

1

2

Couldn't you just extend ArrayCollection and override the contains() method? Alternatively you can paste the source for ArrayCollection into an "mx/collections" package in your project and modify the source; this "monkey-patching technique" will override the behavior throughout your entire project. However I would be extremely cautious about changing ArrayCollection in that manner: since it's used all over the place in the Flex APIs there is a good chance you'll start breaking other components in the framework.

answered Dec 27, 2008 at 17:41

cliff.meyers's user avatar

Comments

2

The contains() method searches by reference, correct (I believe even for primitives), so if you're trying to find a string or an int in an ArrayCollection, you'll have to do the searching yourself, by some variation of looping or searching. I don't think any of us could tell you why there isn't, say, an optional parameter on that method indicating whether to search by ref or by val, though; so it goes, as they say.

But I'd definitely warn you off monkey-patching the framework code -- that's just asking for trouble. :)

answered Dec 31, 2008 at 18:38

Christian Nunciato's user avatar

Comments

0

Well, it seems like the ArrayCollection doesn't actually look directly at memory, but only as a last resort. It will attempt to find a Unique ID (UID) for the object. If the UID doesn't exist, it will create one for it using the UIDUtil.as.

You can get around this whole default UID stuff by having your object implement the IUID interface and providing your own UID for the object. The ArrayCollection will look at the UID you provide it.

answered Apr 2, 2009 at 13:45

DyreSchlock's user avatar

2 Comments

Sorry, that's a negative. I don't know what sources you're looking at, but the ones from 3.2 use "==" to find the item if there's a filterFunction applied and ArrayUtil.getItemIndex ("===") if the ArrayCollection is not sorted. If it is sorted it gets more complicated but there's still no UID checking.

-1: Just tried the proposed usage of the IUID interface, but the implemented methods never get called... unfortunately... It'd be a nice feature though.

0

I would suggest a simple:

in_array($haystack, $arrayCollection->toArray());

answered Feb 10, 2014 at 6:05

Zjoia's user avatar

Zjoia

1763 silver badges16 bronze badges

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.