Ollie B
Ollie B

Reputation: 3

Javascript multidimensional array syntax: what am I doing wrong?

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?

Upvotes: 0

Views: 286

Answers (4)

xdazz
xdazz

Reputation: 160833

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 ].

Upvotes: 7

Mike Robinson
Mike Robinson

Reputation: 25159

"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"]

Upvotes: 2

Anthony Grist
Anthony Grist

Reputation: 38345

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

Upvotes: 1

gen_Eric
gen_Eric

Reputation: 227220

{"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".

Upvotes: 4

Related Questions