最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to access array property on JavaScript object - Stack Overflow

programmeradmin1浏览0评论

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?

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 edited Oct 3, 2013 at 17:32 user2736012 3,54317 silver badges13 bronze badges asked Oct 3, 2013 at 17:20 Marcelo MyaraMarcelo Myara 3,0012 gold badges30 silver badges37 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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)

发布评论

评论列表(0)

  1. 暂无评论