Вопрос

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?

Это было полезно?

Решение

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.

Другие советы

Just

data[0].Opcoes

(attribute Opcoesof the first row of your object)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top