Paddy Hallihan
Paddy Hallihan

Reputation: 1706

accessing array properties

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

I've got this array and want to access the qname property and can't remember how to do it. Is it something like questions[0].qname or questions[0](qname)?

Upvotes: 2

Views: 105

Answers (3)

Neil
Neil

Reputation: 8121

As you tagged ActionScript 3.0, and its a strongly typed language, I would recommend a typed class to hold your data structure.

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

By having getters and setters and also exposing these props in the constructor you can create them in two ways:

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

OR:

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

This offers benefits in terms of intellisense for getting properties in your ide and also type safety to stop you putting in incorrect types for name and value.

Then to get hold of a question:

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

Usually your questions would be coming from some data source, like xml or a web service, whatever I'll use literal xml for this example, in that scenario you would want to build your objects in some loop eg:

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

Then:

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

Upvotes: 0

Rytis Alekna
Rytis Alekna

Reputation: 1377

But if you can't do that what is told in previous answer (e.g. you get those strings from server) you can use regular expresions to get those values nicely:

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

Upvotes: 1

Can Poyrazoğlu
Can Poyrazoğlu

Reputation: 34830

You've defined your array elements as string instead of objects. Try this instead:

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

Curly braces instead of double-quotation marks. With quotation marks you create strings. With curly braces, you can create dynamic objects and set their properties. So if you are creating a string value for qname, make sure you define it as qname:"mc_01" instead of qname:mc_01.

So you can use questions[0].qname or questions[0]["qname"] to access the properties.

Upvotes: 6

Related Questions