Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This is very strange, but I just can't access a property which is an array of objects on a given JavaScript object. I've already outputted on Chrome's console the object itself and the attempt to access the property, and the result leads me to more confusion. Here's what I've received on the Chrome's console about an object present on var named quest:

[Object]
0: Object
    FuncaoValNum: ""
    IDQuestaoMultiplaEscolha: 0
    Opcoes: Array[2]
        0: Object
            IDOpcaoQuestaoMultiplaEscolha: 0
            IDQuestaoMultiplaEscolha: "0"
            Ordem: 0
            Texto: "Op1"
            (...)
            __proto__: Object
        1: Object
            IDOpcaoQuestaoMultiplaEscolha: 0
            IDQuestaoMultiplaEscolha: "0"
            Ordem: 1
            Texto: "Op2"
            (...)
            __proto__: Object
        length: 2
        __proto__: Array[0]
    (...)
    __proto__: Object
    length: 1
__proto__: Array[0]

and on the following line of outputting the above info, I just try to access the object's Opcoes array's length by using quest.Opcoes.length. The result is:

undefined 

Really confused about it as it seems that quest is an object with an array property named Opcoes with 2 other objects on it, and yet, I just can't access it's .length with quest.Opcoes.length or any other way that I could think of.

What am I doing wrong?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

The outermost structure is also an Array, so you need to access the first index of that array to get to the object.

quest[0].Opcoes.length

When you did this:

quest.Opcoes.length

you should actually get a TypeError instead of undefined since quest has no Opcoes property, meaning .length would be accessing a property on undefined.

share|improve this answer
    
Well, don't know why it is outputting an "undefined", but for now, I'm really glad that It worked... I was missing that it was in fact an array. Thanks a LOT! Kind of thing that only an external eye can catch! :D –  Marcelo Myara Oct 3 '13 at 17:30

Just

data[0].Opcoes

(attribute Opcoesof the first row of your object)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.