3 Replies Latest reply on Mar 6, 2011 6:36 PM by crazycabo

    Finding a specific value inside an ArrayCollection

    crazycabo
      public function loadDeck():void {
           var item:Object = collectionGrid.selectedItem;
           var name:String = item.name;
           var deckCount:int;
           var sideCount:int;
           var value:Boolean;
           var temp:String;
                          
           deckCount = 1;
           sideCount = 0;
           deck.addItem({name:name, deckCount:deckCount, sideCount:sideCount});
           value = deck.contains({name:name, deckCount:deckCount, sideCount:sideCount});
      }
      

       

      So, I'm pulling a name from an ArrayCollection "collectionGrid" which I utilize in a dataGrid. I then add it to another ArrayCollection "deck". I would think I could use a method such as "contains" to find that exact item I just added but, value always = false. Why?

       

      What I'd like to be able to do is search for the "name" in the ArrayCollection only and then pull all 3 values from the entry, so that I may change them and replace them. It seems, it's not as easy as I'm imaging it.

        • 1. Re: Finding a specific value inside an ArrayCollection
          jfb00 Level 3

          Hi,

          You can probably filter in your array collection.

          http://cookbooks.adobe.com/post_Using_the_to_ArrayCollection_s_filterFunction-5441.html

          And update your items:

          deck[i].name = "newName";

           

          Rgds

          Johnny

          • 2. Re: Finding a specific value inside an ArrayCollection
            Flex harUI Adobe Employee

            If you have other reasons to sort the ArrayCollection, then you can get an

            IViewCursor and use the find* methods.

             

            Filtering will work, but might be expensive, but so is the sort.

             

            You can also just brute force scan the collection.

             

            You can't use contains() because it does a === test and you have cloned the

            object by making a new instance.

            • 3. Re: Finding a specific value inside an ArrayCollection
              crazycabo Level 1

              After a few days, I figured it out on my own.

               

              // Add cards to the deck or sideboard depending on the value of addType.
                          public function addCardToDeck(addType:String):void {
                              var item:Object = collectionGrid.selectedItem;
                              var cardName:String = item.card_name;
                              var cardType:String = item.type;
                              var setName:String = item.set;
                              var cardRules:String = item.rules_text;
                              var cardNameFromDeck:String;
                              var cardTypeFromDeck:String;
                              var deckCount:int;
                              var sideCount:int;
                              var cardExists:Boolean = false;
                              var heroExists:Boolean = false;
                             
                              // Check if a hero already exists in the deck.
                              for (var loop:int = 0; loop < deck.length; loop++) {
                                  cardTypeFromDeck = deck.getItemAt(loop).type;
                                  if (cardTypeFromDeck == "Hero") {
                                      heroExists = true;
                                      break;
                                  }else {
                                      heroExists = false;
                                  }
                              }
                             
                              /* Loop through the existing deck to see if the name of the card being added is already contained in the deck.
                              If no cards exist in the deck, this loop will not run.*/
                              for (loop = 0; loop < deck.length; loop++) {
                                  cardNameFromDeck = deck.getItemAt(loop).card_name;
                                  if (cardName == cardNameFromDeck) {
                                      cardExists = true;
                                      break;
                                  }else {
                                      cardExists = false;
                                  }
                              }
                             
                              // Add card if it already exists in the deck or sideBoard.
                              if (cardExists) {
                                  // Retrieve card data from the position used in the above loop.
                                  deckCount = deck.getItemAt(loop).deckCount;
                                  sideCount = deck.getItemAt(loop).sideCount;
                                  if (cardType != "Hero") deck.removeItemAt(loop);
                                  if (addType == "deck") {
                                      // Add a card only if the total count of that particular card is not > combined count of the deck and sideboard.
                                      if ((deckCount + sideCount) != 4 && (deckCount + sideCount) < 5) deckCount = deckCount + 1;
                                      if (cardType == "Hero") return; // Do not add card to deck if it is a Hero and already in the deck.
                                  }else {
                                      if (cardType == "Hero") return; // Do not add card to sideboard if it is a Hero.
                                      if ((deckCount + sideCount) != 4 && (deckCount + sideCount) < 5) sideCount = sideCount + 1;
                                  }deck.addItem({card_name:cardName, type:cardType, set:setName, rules_txt:cardRules, deckCount:deckCount, sideCount:sideCount});
                              }else { // Add a new card to the deck or sideboard.
                                  if (addType == "deck" && (heroExists != true && cardType == "Hero")) {
                                      deckCount = 1;
                                  }else if (addType == "deck") {
                                      deckCount = 1;
                                  }else {
                                      if (cardType == "Hero") return; // Do not add card to sideboard if it is a Hero.
                                      sideCount = 1;
                                  }deck.addItem({card_name:cardName, type:cardType, set:setName, rules_txt:cardRules, deckCount:deckCount, sideCount:sideCount});
                              }
                          }