Skip to main content
Vincent-ZHANG
Inspiring
September 27, 2011
Answered

Iteration through array of complex data object on different properties

  • September 27, 2011
  • 3 replies
  • 1179 views

I am not sure my title is correct.

Is there any 3rd-party library on AS3.0( like STL in C++), can do this:

I define my class and use its objects as associative array, for example:

class Company{

      var public name;

     var public logo;

     var public address;

     var public telnumber;

}

And use a kind of data structure class(say, List) to store a few Company-type objects. And then, I define my own comparing and searching function on that List class(namely, override the default one); within my functions I could specify on which attribute(name, or address, or telephone number) to perform searching, and how it would be performed.

Could someone help? Thanks in advance.

This topic has been closed for replies.
Correct answer Andrei1-bKoviI

@Ken: "It's a standard class/method, you don't need to extend or use custom class."

Well, theoretically it is always better to have a specialized datatype instead of using generic Object. Practice is a different story...

Here is a simplified example of how Array.filter() can be used with a custom datatype - in this case it filters Values instances based on vlaue property, well, value.

Place both classes into the same folder and make ValueSort a document class:

package

{

    import flash.display.Sprite;

   

    public class ValueSort extends Sprite

    {

        private var array:Array;

        private var searchValue:int = 0;

       

        public function ValueSort()

        {

            init();

        }

       

        private function init():void

        {

            array = [];

            for (var i:int = 0; i < 10; i++)

            {

                array.push(new Values(i));

            }

            trace("before filter");

            traceArray(array);

            searchValue = 4;

            var searchResult:Array = array.filter(filter);

            trace("after filter");

            traceArray(searchResult);

            searchValue = 7;

            searchResult = array.filter(filter);

            trace("after filter 1");

            traceArray(searchResult);

        }

       

        private function traceArray(arr:Array):void

        {

            for each (var value:Values in arr)

            {

                trace(value.value);

            }

        }

       

        private function filter(element:Values, index:int, arr:Array):Boolean

        {

            return Values(element).value > searchValue;

        }

   

    }

}

package

{

   

    public class Values

    {

        public var value:int = 0;

       

        public function Values(v:int)

        {

            value = v;

        }

   

    }

}

3 replies

Vincent-ZHANG
Inspiring
October 5, 2011

Thanks, Gentlemen.

I will try as you suggest, and go back to you later.

Andrei1-bKoviICorrect answer
Inspiring
September 27, 2011

@Ken: "It's a standard class/method, you don't need to extend or use custom class."

Well, theoretically it is always better to have a specialized datatype instead of using generic Object. Practice is a different story...

Here is a simplified example of how Array.filter() can be used with a custom datatype - in this case it filters Values instances based on vlaue property, well, value.

Place both classes into the same folder and make ValueSort a document class:

package

{

    import flash.display.Sprite;

   

    public class ValueSort extends Sprite

    {

        private var array:Array;

        private var searchValue:int = 0;

       

        public function ValueSort()

        {

            init();

        }

       

        private function init():void

        {

            array = [];

            for (var i:int = 0; i < 10; i++)

            {

                array.push(new Values(i));

            }

            trace("before filter");

            traceArray(array);

            searchValue = 4;

            var searchResult:Array = array.filter(filter);

            trace("after filter");

            traceArray(searchResult);

            searchValue = 7;

            searchResult = array.filter(filter);

            trace("after filter 1");

            traceArray(searchResult);

        }

       

        private function traceArray(arr:Array):void

        {

            for each (var value:Values in arr)

            {

                trace(value.value);

            }

        }

       

        private function filter(element:Values, index:int, arr:Array):Boolean

        {

            return Values(element).value > searchValue;

        }

   

    }

}

package

{

   

    public class Values

    {

        public var value:int = 0;

       

        public function Values(v:int)

        {

            value = v;

        }

   

    }

}

Kenneth Kawamoto
Community Expert
Community Expert
September 28, 2011

Andrei, I was referring to Vector and Array having filter() as standard method, and therefore you don't have to create a custom "List" class to store and filter your data objects like OP was considering. For the OP's case, I'd create Vector.<Company> and push Compnay objects into it, so that you can perform filter(), sort(), etc.

Of course you can also create a custom class that extends Vector or Array if that suits the situation better.

Cheers,

--

Kenneth Kawamoto

http://www.materiaprima.co.uk/

Inspiring
September 28, 2011

Ken,

I obviously misunderstood you post. Sorry man :-)

Kenneth Kawamoto
Community Expert
Community Expert
September 27, 2011

I would use Vector.filter() (or Array.filter()). It's a standard class/method, you don't need to extend or use custom class.

--

Kenneth Kawamoto

http://www.materiaprima.co.uk/