JavaScript JSON Arrays
JavaScript Basics

JavaScript JSON Arrays

JSON Array Literals

This is a JSON string:

'["Ford", "BMW", "Fiat"]'

Inside the JSON string is a JSON array literal:

["Ford", "BMW", "Fiat"]

JSON Arrays

  • 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:

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

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

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

Accessing Array Values

You access array values by index:

const firstCar = myArray[0]; // "Ford"

Arrays in Objects

Objects can contain arrays:

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

You access array values within an object by index:

const firstCar =[0]; // "Ford"

Looping Through an Array

You can loop through array values using a for-in loop:

let text = "";for (let i in {  text +=[i] + " ";}

Or you can use a for loop:

let text = "";for (let i = 0; i <; i++) {  text +=[i] + " ";}

Take a look into your desired course