Solved

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

Posted on 2008-06-17
7
3,414 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
The goal of the tutorial is to teach the user how to how to record live broadcast.
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.

791 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