简体   繁体   English

如何访问JavaScript对象上的数组属性

[英]How to access array property on JavaScript object

This is very strange, but I just can't access a property which is an array of objects on a given JavaScript object. 这很奇怪,但是我只是无法访问一个属性,该属性是给定JavaScript对象上的对象数组。 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. 我已经在Chrome的控制台上输出了对象本身以及访问该属性的尝试,结果使我更加困惑。 Here's what I've received on the Chrome's console about an object present on var named quest : 这是我在Chrome控制台上收到的有关名为quest var存在的对象的信息:

[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 . 在输出上述信息的下一行中,我只是尝试使用quest.Opcoes.length访问对象的Opcoes数组的长度。 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. 对此感到非常困惑,因为quest似乎是一个具有名为Opcoes的数组属性的对象, Opcoes有2个其他对象,但是我只是无法使用quest.Opcoes.length或其他任何方式访问它的.length 。考虑到。

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 . 实际上,您应该得到TypeError而不是undefined因为quest没有Opcoes属性,这意味着.length将访问undefined上的属性。

Just 只是

data[0].Opcoes

(attribute Opcoes of the first row of your object) (将对象第一行的Opcoes归因Opcoes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM