JSON Array Literals
This is a JSON string:
'["Ford", "BMW", "Fiat"]'
Inside the JSON string there is a JSON array literal:
["Ford", "BMW", "Fiat"]
Arrays in JSON are almost the same as arrays in JavaScript.
In JSON, array values must be of type string, number, object, array, boolean or null.
In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.
JavaScript Arrays
You can create a JavaScript array from a literal:
You can create a JavaScript array by parsing a JSON string:
Accessing Array Values
You access array values by index:
Arrays in Objects
Objects can contain arrays:
Example
{
"name":"John",
"age":30,
"cars":["Ford", "BMW", "Fiat"]
}
You access array values by index:
Looping Through an Array
You can access array values by using a for in
loop:
Or you can use a for
loop: