2

I'm curious of the best way to detect that an arraycollection contains duplicate objects as determined by an object's property value. For example,

var _myArrayCollection:ArrayCollection = new ArrayCollection([{name: "name1", value: "value1"}, {name: "name2", value: "value2"}, {name: "name1", value: "value3"}]);

Notice that this arraycollection has 3 items. 2 of the items have the same value for the name property. I would consider this a duplicate.

Any ideas what the body of this method would look like? I have ideas but none of them feel very elegant.

private function containsDuplicates(ac:ArrayCollection, property:String):Boolean

3 Answers 3

3

You could save the values of the given property on an array, and keep searching inside that array for duplicates. The only bad part is that search time increases as the array size does.

var valueArray:Array = new Array();
for each (object:Object in ac) {
    var value = object[property];
    if (valueArray.indexOf(value) >= 0) {
        return true;
    } else {
        valueArray.push(value);
    }
}
return false;

A more efficient (but very more complex solution) may be to save the values, order them, and do a single run to search for duplicates.

Sign up to request clarification or add additional context in comments.

Comments

1
for(var i:int=0; i<buscatype.length; i++)
{
    for(var j:int = (i+1); j <buscatype.length; j++)
    {
        if(buscatype[i].produst_type_nameCol==buscatype[j].produst_type_nameCol)
        {
            buscatype.removeItemAt(j);
            j-=1;
        }
    }
}

1 Comment

Please add some level of description for the code such as stating how it will solve the problem.
1

you can extend the method mentioned in: http://www.fridaymushroom.com/fm/search-an-item-arraycollection-in-flex

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.