bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/JavaScript/Objects, Classes, and Advanced Patterns
JavaScript•Objects, Classes, and Advanced Patterns

JSON Array Literals

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind JSON Array Literals?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

'["___", "BMW", "Fiat"]'
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Looping Through an Array
Accessing Array Values
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

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

Accessing Array Values

You access array values by index

myArray[0];

Arrays in Objects

Example

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

You access array values by index

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];
}

Previous

JSON Object Literals

Next

JSON PHP