1

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?

user2736012
3,54318 silver badges13 bronze badges
asked Oct 3, 2013 at 17:20

2 Answers 2

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.

answered Oct 3, 2013 at 17:22
Sign up to request clarification or add additional context in comments.

1 Comment

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
1

Just

data[0].Opcoes

(attribute Opcoesof the first row of your object)

answered Oct 3, 2013 at 17:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.