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?
-
Well you need square brackets around your values. Are you getting an error?Cfreak– Cfreak2012年03月21日 15:22:09 +00:00Commented 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 :(Karthikeyan Arumugam– Karthikeyan Arumugam2012年03月21日 15:42:54 +00:00Commented Mar 21, 2012 at 15:42
4 Answers 4
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 ].
2 Comments
{"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
"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
You're missing a { to indicate the start of the second object in the array.