Solved

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

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
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 use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

744 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

11 Experts available now in Live!

Get 1:1 Help Now