Eusthace
Eusthace

Reputation: 3861

Coffeescript, array length undefined

Could anyone explain me why the length is always null?

jsCountries = 
  0:
    country: "Brazil"
    photo:  "source.png"


alert jsCountries.length

Upvotes: 3

Views: 16748

Answers (2)

robkuz
robkuz

Reputation: 9944

I dont know exactly what you want to do. If you want to use your code than the access would be

Object.keys(jsCountries).length

If however your intention is to create an array of country objects than I would suggest to initialize your jsCountries var differently

jsCountries = [
   {country: "Brazil", photo:  "source.png"},
   {country: "Argentina", photo:  "aregentina_source.png"},
   ...
]

Then of the following will work

jsCountries.length

In anyway I would strongly suggest not to use numeric keys for an object even thou its possible.

Upvotes: 9

phenomnomnominal
phenomnomnominal

Reputation: 5515

You are declaring an object, with the property 0 set, not an Array.

You want:

jsCountries = [
  country: "Brazil"
  photo:  "source.png"
]

alert jsCountries.length

Upvotes: 6

Related Questions