Solved

How do you loop thru an ArrayCollection and return only unique results?

Posted on 2008-06-17
7
3,413 Views
Last Modified: 2013-11-11
I have an arraycollection from an event result (promotionCol).  The arraycollection returns storeName, storeID, storePromotion.  I want to take the arraycollection and create another collection (storeCol) to be the dataprovider for a comboBox that I will use to filter the results.  The same storeName may be returned many times in the results. Example:

"Apple", "123", "free ipod"
"Apple", "123", "free iphone"
"Buy.com", 456, "free tv"
"Buy.com", 456, "free cellphone"

How do I loop through the intial arraycollection and build a new collection with the storeName only represented once?
[Bindable] private var promotionCol:ArrayCollection = new ArrayCollection;
 
[Bindable] private var storeCol:ArrayCollection;
 
public function onResult(event:ResultEvent):void {
	promotionCol = event.result as ArrayCollection;
 
}

Open in new window

0
Comment
Question by:duanehardy
  • 3
  • 2
7 Comments
 
LVL 10

Accepted Solution

by:
Savong earned 250 total points
ID: 21814022
This is an example of how I approached that issue in a project of mine (slightly modified to hopefully fit your example):
private function uniqueCompany(inCollection:ArrayCollection):void {
   var isNew:Boolean;
   companyCollection = new ArrayCollection();
 
   //I'm assuming an object with companyName as the property you're after
   for each(var tempObj:Object in inCollection) {
      isNew = true;
      for(var i:int=0;i<companyCollection.length;i++){
         if(companyCollection.getItemAt(i).companyName == tempObj.companyName){
            isNew = false;
            break;
         }
      }
 
      if(isNew) {
         companyCollection.addItem(tempObj);
      }
   }
}

Open in new window

0
 

Author Comment

by:duanehardy
ID: 21835969
Thanks for this help.  I'll have feedback soon.  I have one coding problem that has popped up and I have to work on that to get the data out to run through this loop.  Let you know soon.
0
 

Author Comment

by:duanehardy
ID: 21890803
I was unable to get your code to work.  It did get me started in the correct direction.  Here was my final code.

private function StoreName(tempCol:ArrayCollection):void
      {

            var tempObject:Object = tempCol[0];
            storeCol.addItem(tempObject.storeName);
            storeCol.addItem('-- All Stores --');
            
            for (var i:int = 0;i<tempCol.length;i++)
            {
                  for(tempObject in tempCol)
                  {
                        if (storeCol.contains(tempCol[i].storeName) == false)
                        {
                              storeCol.addItem(tempCol[i].storeName);
                        }
                  }
            }
  }
0
 
LVL 10

Expert Comment

by:Savong
ID: 21891419
I'm glad you found your solution, but closing the question indicates that I gave you no help.  In your own response you seemed to indicate that that was not the case.  The solution I gave you is certainly workable, as it's one I use myself.
0
 

Author Comment

by:duanehardy
ID: 21895727
Savong,

I tried to award you half the points (125) for your help.  The only button that I had was to either accept your comment totally or accept my comment.   I had tried your code and received errors.  I worked with it for a while but couldn't fix the errors, so I didn't think I should post it as a total solution.

Your post did get me thinking of a possible solution and some more goggling gave me an example to work from.  Thus I tried to award you half the points.

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
EMET 5.2 / Adobe Flash 2 235
Advertising design for my own Purpose 5 81
Include Adobe AIR libraries in Flash Builder 4 121
IE11 stops playing video on a certain website 10 78
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now