0

在这个例子中:

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"});

“名字”和“出生”有可能成为变量吗?

4

2 回答 2

1

正如@RIAstar 指出的那样,它们是“关联数组”的属性——你的动态对象{}

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);

或者如果是更扩展的版本:

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);
于 2012-08-07T10:27:20.477 回答
0

如果您想创建一个返回给定属性名称的数据的函数,您可以执行以下操作:

        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");

您可以修改它以满足您的需要。

要探索 Array 的功能,请参阅此博客(不是我的)。

于 2012-08-07T17:07:10.313 回答