Is there a more efficient AS3 way to compare 2 arrays for adds, removes & updates?
- by WillyCornbread
Hi all - I'm wondering if there is a better way to approach this than my current solution...
I have a list of items, I then retrieve another list of items. I need to compare the two lists and come up with a list of items that are existing (for update), a list that are not existing in the new list (for removal) and a list of items that are not existing in the old list (for adding).
Here is what I'm doing now - basically creating a lookup object for testing if an item exists. Thanks for any tips.
for each (itm in _oldItems)
{
_oldLookup[itm.itemNumber] = itm;
}
// Loop through items and check if they already exist in the 'old' list
for each (itm in _items)
{
// If an item exists in the old list - push it for update
if (_oldLookup[itm.itemNumber])
{
_itemsToUpdate.push(itm);
}
else // otherwise push it into the items to add
{
_itemsToAdd.push(itm);
}
// remove it from the lookup list - this will leave only
// items for removal remaining in the lookup
delete _oldLookup[itm.itemNumber];
}
// The items remaining in the lookup object have neither been added or updated -
// so they must be for removal - add to list for removal
for each (itm in _oldLookup)
{
_itemsToRemove.push(itm);
}