I ended up using the ObjectUtil.toString and the Dict class. Here's the code
in case it helps anyone.
static public function RemoveDuplicates_tostring(baseArray:Array):Array {
/*
Assumes an array of simple objects
*/
if (baseArray.length > 0) {
var baseDict:Dictionary = new Dictionary(true);
var keyString:String;
var propExcludeArray:Array = new Array("mx_internal_uid");
var returnArray:Array = new Array();
baseArray.forEach( function(item:Object, index:int, array:Array ):void {
keyString =
ObjectUtil.toString(item,null,propExcludeArray);
baseDict[keyString] = item;
}
);
for each (var item:Object in baseDict) {
returnArray.push(item);
}
return returnArray;
}
else
return baseArray
}
----- Original Message -----
From: Warren
To: [email protected]
Sent: Thursday, July 01, 2010 9:25 PM
Subject: Re: [flexcoders] How to remove duplicate objects from an array of
objects
Yup -- I'm using AMF.
What you say makes good sense. Using the bytearray coould be risky
especially since I can't depend on the property order always being the same. I
haven't done much OO yet but I see your point about long term maintenance.
The valueOf trick reminds me a lot of what I was doing with the
ObjectUtil.toString(). As long as the objects were simple, I would get a clean
consistent string.
Hmm -- Maybe using the dict object wasn't such a bad idea after all. It was
extremely simple and would probably do the trick in my environment. I have a
lot of control over user entry. I'm using restrict on the input fields and
doing additional cleanup in the backend as part of my oracle stored procs.
There won't be odd characters in the data.
This is proving to be a good learning experience. I haven't had to dig this
deep into object comparison techniques. I'm sure glad you have your act
together and are willing to share.
----- Original Message -----
From: Oleg Sivokon
To: [email protected]
Sent: Thursday, July 01, 2010 8:41 PM
Subject: Re: [flexcoders] How to remove duplicate objects from an array of
objects
Well, then my take would be this: since you are sending them from CF, you
are probably using AMF, and since you do so, you can use strongly typed
objects, and (again, I'm not sure but...) I think that strongly typed objects
are written accordingly to their describeType - this means objects are going to
be consistent when serialized. But, the thing with comparing serialized objects
is more of a trick, then a really good design. I would go for some kind of
equals():Boolean method for those objects and implement it in a way it would
only compare the relevant data.
Something more like this:
class BusinessObject {
public function equals(object:BusinessObject):Boolean {
if (this.foo != object.foo) return false;
if (this.bar != object.bar) return false;
return true;
}
}
This would cover all the cases, like when you want the objects to have only
some unique properties, but other you don't mind. It will be also easier to
maintain in the long run. This is an inherent problem of OO languages vs
rrelational database languages, where the first implies that objects must have
unique identity and the second believes that the objects that have all the same
traits are the same. Of course this is not black and white, but it's a known
problem. The equals-like method is probably the standard kind of solution to
this problem, unless you can find some more optimized way to do it.
There are more interesting things, for example, if you implement valueOf()
method of an object in a way that it returns number or string, the implementors
may be treated under certain circumstances as being of simple type, so, say and
you have something like this:
class BusinessObject {
public function valueOf():Object {
return int(this.foo + this.bar);
}
}
Then you can cast BusinessObject to int and compare ints :)
var bo0:BusinessObject = new BusinessObject(100, 200);
var bo1:BusinessObject = new BusinessObject(100, 300);
if (int(bo0) === int(bo1)) // objects are assumed equal.
This approach may be more efficient if you cache the valueOf value in the
BusinessObject so that when called it only returns the value.