You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@flex.apache.org by jfb <j....@verizon.net> on 2016/09/07 19:16:51 UTC

Consolidate items from ArrayCollection

Hi All,
How can I consolidate or merge the same items from an arrayCollection into
one but summary the fields with numbers?
example:
[Bindable] private var acSample:ArrayCollection = new ArrayCollection([
			{Fname: "Kranthi", Lname:"Kata", dob:"21/10/1972", Amount_1:10000,
Amount_2:10},
			{Fname: "Vasanth", Lname:"Lola", dob:"12/01/1980", Amount_1:5000, 
Amount_2:20},
			{Fname: "Vasanth", Lname:"Lola", dob:"12/01/1980", Amount_1:5000, 
Amount_2:30},
			{Fname: "Sample1", Lname:"Lola", dob:"12/01/1982", Amount_1:10000,
Amount_2:40}
]);

The result arrayCollection should display 3 unique records, but sum Amount_1
and Amount_2 for record Vasanth

I found this:

 public static function
getDisticntArrayCollection(sourceCollection:ArrayCollection):ArrayCollection{
      var distinctValuesCollection:ArrayCollection = new ArrayCollection();
      for each(var item:* in sourceCollection){
          var propertyValue:Object = item.VALUE;
          if(distinctValuesCollection.getItemIndex(propertyValue) == -1)
             distinctValuesCollection.addItem(item);
       }
    return distinctValuesCollection;
  }

I try to replace the item.VALUE with Fname but always get the same records.
thanks for your help in advanced.
Best,




--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Consolidate-items-from-ArrayCollection-tp13509.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.

Re: Consolidate items from ArrayCollection

Posted by Nemi <ne...@gmail.com>.
Faster way would be to use ArrayCollection.source Array to loop through and
manipulate data.
Then when you got resultArray do: acResult = new
ArrayCollection(resultArray);

That way you don't need refresh(), if you do, then call it only once at the
end. Don't call it from loop.

To make it more clean, and also possible faster, you can make new Class
(valueObject) in order to type objects like:
{Fname: "Kranthi", Lname:"Kata", dob:"21/10/1972", Amount_1:10000,
Amount_2:10}

So, for example, instead of:
acResult.getItemAt(j)["Amount_1"] += ac.getItemAt(i)["Amount_1"];

in the end you can have:
myDataObj = acResult[j];
myDataObj.Amount_1 += srcArray[i].Amount_1;





--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Consolidate-items-from-ArrayCollection-tp13509p13525.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.

Re: Consolidate items from ArrayCollection

Posted by jfb <j....@verizon.net>.
Thanks for your reply and help.
I got it, my only concern is that I have 40K records and i hope this is a
fast way.

[Bindable] private var acResult:ArrayCollection = new ArrayCollection();

private function groupAndSumArray(ac:ArrayCollection, key:String):void
			{
				var acLength:Number = ac.length;
				var flag: Boolean;
				
				for (var i: int = 0; i < acLength; i++) {
					flag = false;
					for (var j: int = 0; j < acResult.length; j++) {
						if (acResult.getItemAt(j)[key] == ac.getItemAt(i)[key]) {
							acResult.getItemAt(j)["Amount_1"] += ac.getItemAt(i)["Amount_1"];
							flag = true;
							break;
						}
					}
					if (!flag) {
						acResult.addItem(ac.getItemAt(i));
						acResult.refresh();
					}
				}
			}



--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Consolidate-items-from-ArrayCollection-tp13509p13517.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.

Re: Consolidate items from ArrayCollection

Posted by OK <po...@olafkrueger.net>.
This is a collection of different aproaches:
http://stackoverflow.com/questions/5997822/flash-as3-how-do-i-remove-duplicates-in-an-array

HTH,
Olaf



--
View this message in context: http://apache-flex-users.2333346.n4.nabble.com/Consolidate-items-from-ArrayCollection-tp13509p13515.html
Sent from the Apache Flex Users mailing list archive at Nabble.com.