0

I want to create an object with nested objects inside of it whose objects has objects inside of those, like this simple structure:

var obj:Object = {
  exercise : {
    question0 : {}
    question1 : {}
    question2 : {}
    question3 : {}
  }
};

Then assigning those objects their properties dynamically with the contents of an XML like this:

for (var i:uint=0; i<4; i++) {
                    
  obj.exercise["question"+i].original = varXML.texts.exercise.question[i].original.@text;
  obj.exercise["question"+i].example = varXML.texts.exercise.question[i].example.@text;
  obj.exercise["question"+i].answer = varXML.texts.exercise.question[i].answer.@text;
}

This code works well; that's the way I know. However the number of question objects is dynamic and I'm not able to find a way to define the structure of the main object. So I don't want to have question0, question1, question2, question3, question4, etc declared from the beginning (without that I get an error).

Just doing this: var objSenTense:Object = {}; doesn't help either. How can I do this?

3
  • Can't you just have exercise be an array instead of an object? Commented May 7, 2013 at 15:46
  • This example is just a simplified way of what I really need to do, so I need them to be objects in my complete code. Thanks Commented May 7, 2013 at 15:48
  • 1
    You could just have { exercise : { data: ..., otherdata: ..., questions: []} } Commented May 7, 2013 at 16:02

3 Answers 3

1

You can force the creation of the object before editing it:

if (!obj.exercise.hasOwnProperty("question"+i))
{
   obj.exercise["question"+i] = {};
}
obj.exercise["question"+i].original = varXML.texts.exercise.question[i].original.@text;
obj.exercise["question"+i].example = varXML.texts.exercise.question[i].example.@text;
obj.exercise["question"+i].answer = varXML.texts.exercise.question[i].answer.@text;
Sign up to request clarification or add additional context in comments.

2 Comments

Seems interesting but I don't get it, can you please explain some more? Thanks
You can leave your exercise object empty, the code tests if the object question? (where ? is the index) exists in exercise and if not it creates it, you won't get any error with that.
1

Why not use an array of questions?

var obj:Object = {
  exercise : {
    questions:[]
  }
};

for (var i:uint=0; i<4; i++) {

    var question:Object = {};
    question.original = varXML.texts.exercise.question[i].original.@text;
    question.example = varXML.texts.exercise.question[i].example.@text;
    question.answer = varXML.texts.exercise.question[i].answer.@text;
    obj.exercise.questions.push(question);
}

1 Comment

Surely this works, however the structure of my real code is more complex, so my example was just a representative minimal example. I really need them to be objects. Thanks for your answer
1

To get number of questions from xml, use this code:

var length = varXml.texts.exercise.question.length();

for (var i:uint = 0; i < length; i++) { ...

Inside the loop you can define as many question objects as you want like this:

obj.exercise["question"+i] = {};

Therefore the initial structure of your obj could be as simple as this:

var obj = {exercise: {}};

Although it would make more sense to use array for questions, not object.

1 Comment

Thanks I already know it, is a much better way, I have it like that in my real code. However my problem is with the question0, question1, question2, question3..... part. Thanks for your help though

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.