A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Searching through an array of objects

  1. #1
    Member
    Join Date
    Nov 2007
    Posts
    41

    Searching through an array of objects

    Hi all - I have got an array of objects, each with properties, plus those objects have sub objects.

    example

    Code:
            Array[0]
                    Object.id = 0
                    Object.name = fish
                    Object.Array[0]
                              object.id = 0
                              object.variety = red tetra
                              object.size = small
    And so I have a large amount of Objects in my Array comprised of lots of objects as well. I want to now search this Array for specific matching items and pull them into another array of objects.

    The start I have made is

    Code:
    public class SearchFish extends Sprite
    	{
    		private var _searchedAnimalArrayResult:Array = new Array();
    		private var _variety:String;
    		private var _size:String;
    		
    		public function SearchAnimals()
    		{
    			super();
    		}
    		private function searchAnimalArray(arrayToSearchOn:Array, variety:String, size:String) {
    			_variety= variety;
    			_size= size;
    			_searchedAnimalArrayResult= arrayToSearchOn.forEach(filterArray(_variety, _size));
    		}
    		private function filterArray(variety, size){
    			
    		}
    	}
    So now it will go through every item in the array and work filterArray on it, but where to proceed from here?

    cheers

    frank

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Here's what I'd start with - not sure the exact details you want but this setup should be pretty easy to modify. (Also if you know the structure of all the objects first you can take out the hasOwnProperty checks - that will speed it up a lot).

    PHP Code:
    var foundFishes:Array = [];

    for 
    each(var obj:Object in arrayToSearchOn){
        if(
    obj.hasOwnProperty("Array")){
            for(var 
    innerObj:Object in obj.Array){
                if(
    innerObj.hasOwnProperty("variety")){
                    if(
    innerObj.variety == varietyfoundFishes.push(obj);
                }
            }
        }


  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You have a few problems there.

    First, your constructor is SearchAnimals, but your class is SearchFish. Change one to match the other.

    Second, your searchAnimalArray method doesn't return anything. It will at least have to return _searchedAnimalArrayResult.

    Third, forEach takes a function argument, but you are passing the result of calling filterArray. That is not a function. At the moment, it is nothing at all.

    Finally, this doesn't seem to be a class to me. It could be a utility class full of static functions, or these functions could just live in the package, or they could be part of some other class. I'll go with the first.

    Is your structure recursive, or just the two levels you have? I'll assume it's just the two levels, since they have different properties.

    Here is an iterative solution.
    Code:
    public class SearchAnimals{
    
       public static function searchAnimalArray(arrayToSearchOn:Array, variety:String, size:String):Array{
         var toreturn:Array = [];
         for (var i:int = 0; i < arrayToSearchOn.length; i++){
            var subArray:Array = arrayToSearchOn[i].things; //dunno what your sub array property is.
            for (var j:int = 0; j < subArray.length; j++){
               var item:Object = subArray[j];
               if ((item.variety == variety) && (item.size == size)){
                 toreturn.push(item);
               }
            }
         }
         return toreturn;
       }
    }
    And here is a functional style solution:
    Code:
    public class SearchAnimals{
    
       public static function searchAnimalArray(arrayToSearchOn:Array, variety:String, size:String):Array{
         var toreturn:Array = [];
         arrayToSearchOn.forEach(filterSubArray(variety, size, toreturn));
         return toreturn;
       }
    
       /**
        * returns a function to use as the callback for forEach.  Returned function
        * takes an object, filters its subarray, and adds matching stuff to the accumulator
        */
       private static function filterSubArray(variety:String, size:String, accumulator:Array):Function{
          return function(item:*, index:int, array:Array):void{
             var subArray:Array = item.things; //don't know the property for your sub-array
             accumulator.concat(subArray.filter(function(o:*, idx:int; arr:Array):Boolean{
                  return ((o.variety == variety) && (o.size == size));
               }));
          }
       }
    
    }
    Last edited by 5TonsOfFlax; 08-12-2009 at 10:27 AM.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Looks like neznein9 and I interpreted your requirements differently. Did you want the outer objects in the final result, or the inner objects?

  5. #5
    Member
    Join Date
    Nov 2007
    Posts
    41
    Thanks guys for the reply - lol and again my ineptitude at as3 comes under the blow torch of flax's feedback - have you considered writing a book because I'd buy it!

    Ok I confess my original code is shabby simply because I was trying to simplify my requirements to get a better understanding of the code.

    The purpose of the code is not to do with animals nor fish - I try to use basic information so as to not confuse the look of the code - but to find matching computers in an array of computer product Objects, each with properties of price, brand, model etc, complete with sub objects for features, such as ram, video cards, which in turn have an attribute object - ie VRAM, RAMDAC etc.

    The ui of the project wants to send in, at this time, only two search parameters on this array of computers - RAM and Hard Drive.

    My idea was to firstly search through the products for all computers matching the RAM or higher requirement and push those into a new array. Then pass this new array onto a second function that looks for matching hdd or higher requirement. In the end we end up with an array of computers with RAM and HDD amount matching or more than the search amounts. You see why I started with fish? :P

    So by going by the two great posts, I've created my SearchProducts class (which the constructor named properly doh), am just trying to test using Flex Builders damaged debugger

    cheers guys

    frank

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center