0

I'm really struggling to create a valid multidimensional JavaScript array with the following basic format:

var countries = [
  {
    "country": "UK",
    "properties": {"value1", "value2", "value3"}
  },
    "country": "Spain",
    "properties": {"value4", "value5", "value6"}
  }
]

Can someone tell me what I'm doing wrong please?

2
  • Well you need square brackets around your values. Are you getting an error? Commented Mar 21, 2012 at 15:22
  • your array declaration itself is wrong... please try to visit w3c schools atleast and get some js basics.... "country":"uk" what are try to do with this line ? trying make key value pair ? country as key and uk as value ??? and you missing some brackets too :( Commented Mar 21, 2012 at 15:42

4 Answers 4

7

Please check the below:

var countries = [
  {
    "country": "UK",
    "properties": ["value1", "value2", "value3"]
  },
  {
     "country": "Spain",
     "properties": ["value4", "value5", "value6"]
  }
]

countries is a array, which has 2 element, and the element is an object, whose properties looks like also an array, the array syntax is like [1,2,3]. And be sure { and [ should be pair with } and ].

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

2 Comments

If they can't see the errors in their own code, they're not necessarily going to spot the difference in yours. Some indication of what they're doing wrong probably wouldn't go amiss.
Thanks, I found this blindingly obvious bug myself and then went to delete my question... but it was too late. Can you tell I'm having a bad day?
4
{"value1", "value2", "value3"}

If this is to be an array, the {} should be [].

{} makes an object, which needs to be key/value pairs.

You're also missing a { before "country": "Spain".

1 Comment

@jbabey...Actually Anthony Grist noticed that, I added that to my answer after reading his answer =/
2
"properties": {"value1", "value2", "value3"}

This is an object which requires key / value pairs. So you can either do:

"properties": {"value1": "value1", "value2": "value2", "value3": "value3"}

(Which is kind of silly). Or you can use an array:

"properties": ["value1", "value2", "value3"]

Comments

1

You're missing a { to indicate the start of the second object in the array.

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.