Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Coffeescript,数组长度未定义_Arrays_Coffeescript - Fatal编程技术网

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”}]