Arrays Coffeescript,数组长度未定义
谁能解释一下为什么长度总是空的Arrays Coffeescript,数组长度未定义,arrays,coffeescript,Arrays,Coffeescript,谁能解释一下为什么长度总是空的 jsCountries = 0: country: "Brazil" photo: "source.png" alert jsCountries.length 您正在声明一个属性设置为0的对象,而不是数组 你想要: jsCountries = [ country: "Brazil" photo: "source.png" ] alert jsCountries.length 我不知道你到底想做什么。如果您想使用您的代码,
jsCountries =
0:
country: "Brazil"
photo: "source.png"
alert jsCountries.length
您正在声明一个属性设置为
0
的对象,而不是数组
你想要:
jsCountries = [
country: "Brazil"
photo: "source.png"
]
alert jsCountries.length
我不知道你到底想做什么。如果您想使用您的代码,那么访问权限将是
Object.keys(jsCountries).length
但是,如果您打算创建一个country对象数组,那么我建议您以不同的方式初始化jsCountries var
jsCountries = [
{country: "Brazil", photo: "source.png"},
{country: "Argentina", photo: "aregentina_source.png"},
...
]
然后,以下其中一项将起作用
jsCountries.length
无论如何,我强烈建议不要对对象使用数字键,即使可能。谢谢您的帮助。我最后使用了:jsCountries=[{国家:“阿尔巴尼亚”,照片:“svg.png”}]