3

Could anyone explain me why the length is always null?

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


alert jsCountries.length

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your help. i've used finally: jsCountries = [ { country: "Albania", photo: "svg.png" } ]
6

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

You want:

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

alert jsCountries.length

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.