Re: [Flashcoders] Data merging problem
I am not sure why you have duplicate items. I am assuming these items come from a database, you can use the query to leave out double records. In most other cases, the double is in there for a reason, which means you do not want to delete them. In that case you would like the .filterFunction property. The filterFunction property is used to make sure an item is not displayed while it is in the collection, this is used as follows: *arrayCollection.filterFunction = removeDuplicate; function removeDuplicate(item:Object):Boolean { /* Do you magic here. More then one option exists. You could create fingerprint of your object here and store it in an object as a key, like this: var ba:ByteArray = new ByteArray(); ba.writeObject(item); var fingerprint:String = ba.toString(); if (checkObject.hasOwnProperty(fingerprint)) { keepItem = false; } else { checkObject[fingerprint] = true; keepItem = true; }; The above method only works if you data does not contain an ID. Another option (which might be faster in most cases, testing required) is to loop over the collection and check if the items that contain the same data are infact the same objects. In this case I would not use a for loop, but a while(i--) loop since these tend to be a bit faster and order does not matter in this case. */ };* In order to apply the filter use the .refresh method of the array collection. Greetz Erik On 5/9/08, Juan Pablo Califano <[EMAIL PROTECTED]> wrote: > > If you're going to remove elements with a splice, you should loop backwards > (for var i:Number = array.length - 1;i >= 0; i++), because when you loop > forward and take out one element, the relative position of the next items > will change. > > Cheers > Juan Pablo Califano > > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Data merging problem
If you're going to remove elements with a splice, you should loop backwards (for var i:Number = array.length - 1;i >= 0; i++), because when you loop forward and take out one element, the relative position of the next items will change. Cheers Juan Pablo Califano 2008/5/9, Jason Van Pelt <[EMAIL PROTECTED]>: > > This is untested so it may need a bit of tweaking, but you could do > something like this: > > // for each item in the array > for(var i = 0; i < workData.length; i++){ > > // compare to all other array elements > for(var j = 0; j < workData.length; j++){ > >// do your comparison(s), making sure not to compare to itself >if(workData[i].pm == workData[j].pm && i != j){ > > // overwrite the propeties of the first element with the > properties of the second > for(var props in workData[i]){ >workData[i][props] = workData[j][props]; > } > > workData.splice(1,j); >} > } > } > > > Things to consider-- > Will all elements in your array have the same properties? > Do you need more logic involved in figuring out which properties should be > the ones to keep? > > > Jason Van Pelt > Interactive Developer > 504.210.1232 (p) / 504.581.2731 (f) > Peter A. Mayer Advertising, Inc. > www.peteramayer.com > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Data merging problem
This is untested so it may need a bit of tweaking, but you could do something like this: // for each item in the array for(var i = 0; i < workData.length; i++){ // compare to all other array elements for(var j = 0; j < workData.length; j++){ // do your comparison(s), making sure not to compare to itself if(workData[i].pm == workData[j].pm && i != j){ // overwrite the propeties of the first element with the properties of the second for(var props in workData[i]){ workData[i][props] = workData[j][props]; } workData.splice(1,j); } } } Things to consider-- Will all elements in your array have the same properties? Do you need more logic involved in figuring out which properties should be the ones to keep? Jason Van Pelt Interactive Developer 504.210.1232 (p) / 504.581.2731 (f) Peter A. Mayer Advertising, Inc. www.peteramayer.com ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Data merging problem
Remove the entry from the array? workData.splice(1, 1); -- Eduardo Omine http://blog.omine.net/ http://www.omine.net/ ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
[Flashcoders] Data merging problem
Hi All, I am dealing with a puzzle here, and I can't figure out the best way to approach this. I come to you all to see what your thoughts are. I will try my best to explain this, because I am confused, I can't guarantee it will at all make sense. I have an ArrayCollection, "workData" that has a list of employee work information. I want to merge two of the same data together. workData[0].pm = "Bob"; workData[0].resource = "Anthony" workData[0].client = "Microsoft" workData[0].role = "Developer" workData[0].job = "XXX-" workData[0].week_date = 7 workData[0].totalHours = 40 workData[1].pm = "Bob"; workData[1].resource = "Anthony" workData[1].client = "Microsoft" workData[1].role = "Developer" workData[1].job = "XXX-" workData[1].week_date = 14 workData[1].totalHours = 32 workData[2].pm = "Tom"; workData[2].resource = "Anthony" workData[2].client = "Microsoft" workData[2].role = "Developer" workData[2].job = "XXX-" workData[2].week_date = 14 workData[2].totalHours = 52 The ArrayCollection displayed here has two matching items, 1 and 2. I want to merge the two because everything matches except for the week_date and totalHours, which is fine, that will be my next problem to solve. I want to create a new ArrayCollection to have 1 and 3 show, while having 1 and 2 merged. What is best way of achieving this? I'm basically taking this data and displaying it into a datagrid, but I don't want the duplicated items to display on the datagrid. So, instead of 3 items to display, I want to display two. -- Anthony Cintron Flash || Flex Developer Blog - http://codegasm.blogspot.com/ Portfolio - http://www.sweetiesandgangsters.com [EMAIL PROTECTED] ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders