Solved

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

Posted on 2008-06-17
7
3,417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.

691 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