0

In this example:

var poets:Array = new Array();
poets.push({name:"Angelou", born:"1928"});
poets.push({name:"Blake", born:"1757"});
poets.push({name:"cummings", born:"1894"});
poets.push({name:"Dante", born:"1265"});
poets.push({name:"Wang", born:"701"});

Is it possible for 'name' and 'born' to be variables?

0

2 Answers 2

1

As @RIAstar points out, they are properties of an 'associative array' - your dynamic Object{}:

var poets:Array = new Array();
poets.push({"name":"test","born":"1928"});
poets.push({name:"Angelou", born:"1928"});
poets.push({name:"Blake", born:"1757"});
poets.push({name:"cummings", born:"1894"});
poets.push({name:"Dante", born:"1265"});
poets.push({name:"Wang", born:"701"});

trace(poets[0].name,poets[0].born);

or if a more expanded version:

var prop1:String = "name";
var prop2:String = "born";
var poets:Array = [];
poets[0] = {};
poets[0][prop1] = "test2";
poets[0][prop2] = "1900";

trace(poets[0].name,poets[0].born);
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, well, I meant, can I create a function, which would create an array where I could pass the names of attributes it should create the array with? So that one call to this function will create an array with properties name, born. And another call will create an array with surname, died. Is it possible?
I don't why that's not possible. You can do this as rigid or as flexible as you need, but keep in mind dynamic objects use more resources. One way is to tackle this is to create a basic class(VO) to store this data: package{ class Poet{ public var firstName:String;public var lastName:String;public var yearBorn:int;public var yearDied:int; public function(f:String,n:String,b:int,d:int){firstName = f;lastName = l;yearBorn=b;yearDied=d;}} } and create an array/Vector.<Poet> and pass your data into the constructor. Another is to use Dynamic Object instances as you do and somewhere along these..
...lines it's also probably worth mentioning the XML object e.g.<poets> <poet name={nameVar} born={yearVar} /> ... </poets> and remember that Flash Player 11 now has native JSON support. In the end it depends where your data comes from and it's going to (what you do with it) so it's totally up to you to find the best option for your setup.
0

If you want to create a function that returns data given the attribute name, you can do something like this:

        public function getDataByAttribute(fieldName:String):Array {
            return poets.map(
                function (item:*, index:int, array:Array):String {
                    return item[fieldName];                         
                }   
            );
        }

        // sample call  
        var results:Array = getDataByAttribute("born");

You can modify it to suit your needs.

To explore Array's functions, see this blog (not mine).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.