Arrays of Objects
Your phone doesn't have one contact. It has hundreds: a list of contacts, each with its own name and number. A list where every item is an object is one of the most common shapes of data.
An array of objects
const friends = [
{ name: "Sam", age: 13 },
{ name: "Lea", age: 12 },
{ name: "Ola", age: 14 },
];
Square brackets for the list, and curly brackets for each item. Every item has the same properties.
Reading one
First the item, by its index, then its property:
console.log(friends[1].name);
Lea
friends[1] is Lea's object, and .name is her name.
A loop through all of them
for (const friend of friends) {
console.log(`${friend.name} is ${friend.age}`);
}
Sam is 13
Lea is 12
Ola is 14
In each lap, friend is one whole object, and you read the properties you need.
Break it on purpose
Forget the index:
console.log(friends.name);
undefined
The array has no name: only each item has one. First choose an item, then its property.
Mission: Tomorrow's flights
What you will see: when schedule.html opens, the console shows:
Tomorrow: SL-201 to Moon Base
Tomorrow: SL-202 to Titan Dock
Tomorrow: SL-203 to Mars Relay
Tomorrow's three flights are already planned, and the script keeps them as a list of objects.
- In
schedule.js, one array holds three objects. - Each object has a
codeand adestination:SL-201toMoon Base,SL-202toTitan Dock,SL-203toMars Relay. - A loop writes
Tomorrow:, each flight's code,to, and its destination.