简体   繁体   English

JavaScript对象的属性未定义

[英]JavaScript object's properties undefined

I have an object that has properties, but when I try to access them, it always returns undefined. 我有一个具有属性的对象,但是当我尝试访问它们时,它总是返回未定义的。

Using Chrome, I attached the object to window and in the console, if I do, window.country 使用Chrome,我将该对象附加到window,然后在控制台中将window.country附加到控制台

It returns this object: 它返回这个对象:

[ Object
currency_code: null
iso: "USA"
iso3: "USA"
name: "USA"
states: Array[51]
__proto__: Object
]

However, when I try to access a property, such as window.country.iso it's undefined. 但是,当我尝试访问诸如window.country.iso之类的属性时,它是未定义的。 Any ideas why this would be? 任何想法为什么会这样?

Thank you! 谢谢!

Your window.country object is actually an array. 您的window.country对象实际上是一个数组。

Chrome's console is showing you that. Chrome的控制台向您显示。 As you can see your object is wrapped in [] , that means it's in an array. 如您所见,对象包装在[] ,这意味着它在数组中。

Try to access it like: window.country[0].iso . 尝试像以下方式访问它: window.country[0].iso

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

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