?
Solved

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

Posted on 2008-06-17
7
Medium Priority
?
3,423 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 1000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
Suggested Courses

762 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