JSON Arrays

This is a JSON string

JavaScript Arrays

You can create a JavaScript array from a literal:

Example

myArray = ["Ford", "BMW", "Fiat"];

You can create a JavaScript array by parsing a JSON string:

Example

myJSON = '["Ford", "BMW", "Fiat"]';
myArray = JSON.Parse(myJSON);

Accessing Array Values

You access array values by index:

Example

myArray[0];

Arrays in Objects

Objects can contain arrays:

Example

{
"name":"John",
"age":30,
"cars":["Ford", "BMW", "Fiat"]
}

You access array values by index:

Example

myObj.cars[0];

Looping Through an Array

You can access array values by using a for in loop:

Example

for (let i in myObj.cars) {
  x += myObj.cars[i];
}

Or you can use a for loop:

Example

for (let i = 0; i < myObj.cars.length; i++) {
  x += myObj.cars[i];
}