访问数组属性

时间:2012-08-28 19:51:34

标签: arrays actionscript-3

var questions:Array = new Array;
questions[0] = "qname:mc_01, qvalue:1";
questions[1] = "qname:mc_02, qvalue:1";
questions[2] = "qname:mc_03, qvalue:1";
questions[3] = "qname:mc_04, qvalue:1";
questions[4] = "qname:mc_05, qvalue:1";
questions[5] = "qname:mc_06, qvalue:1";
questions[6] = "qname:mc_07, qvalue:1";
questions[7] = "qname:mc_08, qvalue:1";
questions[8] = "qname:mc_09, qvalue:1";
questions[9] = "qname:mc_10, qvalue:1";
questions[10] = "qname:mc_11, qvalue:2";
questions[11] = "qname:mc_12, qvalue:2";
questions[12] = "qname:mc_13, qvalue:2";
questions[13] = "qname:mc_14, qvalue:2";
questions[14] = "qname:mc_15, qvalue:2";
questions[15] = "qname:mc_16, qvalue:2";
questions[16] = "qname:mc_17, qvalue:2";
questions[17] = "qname:mc_18, qvalue:2";
questions[18] = "qname:mc_19, qvalue:2";
questions[19] = "qname:mc_20, qvalue:2";
questions[20] = "qname:mc_21, qvalue:3";
questions[21] = "qname:mc_22, qvalue:3";
questions[22] = "qname:mc_23, qvalue:3";
questions[23] = "qname:mc_24, qvalue:3";
questions[24] = "qname:mc_25, qvalue:3";
questions[25] = "qname:mc_26, qvalue:3";
questions[26] = "qname:mc_27, qvalue:3";
questions[27] = "qname:mc_28, qvalue:3";
questions[28] = "qname:mc_29, qvalue:3";
questions[29] = "qname:mc_30, qvalue:3";

我有这个数组,想要访问qname属性,不记得怎么做了。它是questions[0].qname还是questions[0](qname)

3 个答案:

答案 0 :(得分:6)

您已将数组元素定义为字符串而不是对象。 试试这个:

var questions:Array = new Array;
questions[0] = {qname:mc_01, qvalue:1};
...

用大括号代替双引号。使用引号可以创建字符串。使用花括号,您可以创建动态对象并设置其属性。因此,如果要为qname创建字符串值,请确保将其定义为qname:"mc_01"而不是qname:mc_01

因此,您可以使用questions[0].qnamequestions[0]["qname"]来访问属性。

答案 1 :(得分:1)

但是如果你不能这样做,那么在前一个答案中就会说出来(例如你从服务器那里得到这些字符串),你可以使用常规表达来很好地获得这些值:

var searchPattern : RegExp = /(?P<qname>(?<=qname\:)[a-zA-Z0-9_]+(?=[\s,]*))/g;
trace( searchPattern.exec(questions[1]).qname ); // traces out: mc_02

答案 2 :(得分:0)

在标记ActionScript 3.0及其强类型语言时,我建议使用类型化的类来保存数据结构。

package your.package.name
{
    public class Question
    {
        protected var _name:String;
        protected var _value:String;

        public function Question(name:String = null, value:String = null)
        {
            this.name = name;
            this.value = value;
        }

        public function get name():String 
        {
            return _name;
        }

        public function set name(value:String):void 
        {
            _name = value;
        }

        public function get value():String 
        {
            return _value;
        }

        public function set value(value:String):void 
        {
            _value = value;
        }
    }
}

通过使用getter和setter以及在构造函数中公开这些props,您可以通过两种方式创建它们:

var question:Question = new Question("Question1", "Question value");

OR:

var question:Question = new Question();
question.name = "Question1";
question.value = "Question value";

这提供了智能感知方面的优势,可以在您的ide中获取属性,还可以输入安全性来阻止您为名称和值输入错误的类型。

然后抓住一个问题:

questions[0].name; // in this example Question1
questions[0].value; // in this example Question value

通常你的问题来自某些数据源,比如xml或web服务,无论我在这个例子中使用文字xml,你都希望在某个循环中构建你的对象,例如:

var questionsXML:XML = 
<questions>
    <question name="Question1">Question1 value</question>
    <question name="Question2">Question2 value</question>
    <question name="Question3">Question3 value</question>
</questions>

然后:

var questions:Array = [];

for each (var questionXML:XML in questionsXML.question) 
{
    var question:Question = new Question();
    question.name = questionXML.@name;
    question.value = questionXML.text();
    questions.push(question);
}