任何人都可以解釋爲什麼長度始終爲空?Coffeescript,數組長度undefined
jsCountries =
0:
country: "Brazil"
photo: "source.png"
alert jsCountries.length
任何人都可以解釋爲什麼長度始終爲空?Coffeescript,數組長度undefined
jsCountries =
0:
country: "Brazil"
photo: "source.png"
alert jsCountries.length
我不知道你想做什麼。如果你想用你的代碼,比接入將是
Object.keys(jsCountries).length
然而,如果你的目的是建立國家對象的數組比我會建議初始化jsCountries變種不同
jsCountries = [
{country: "Brazil", photo: "source.png"},
{country: "Argentina", photo: "aregentina_source.png"},
...
]
隨後的以下將工作
jsCountries.length
無論如何,我強烈建議不要使用數字鍵的對象,即使你可能。
您正在聲明一個對象,其屬性0
已設置,而不是數組。
你想:
jsCountries = [
country: "Brazil"
photo: "source.png"
]
alert jsCountries.length
謝謝你的幫助! – Eusthace
謝謝你的幫助。我終於使用了:jsCountries = [{country:「Albania」,photo:「svg.png」}] – Eusthace