Objects Containing Arrays
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Arrays Inside an Object
Imagine describing a single Codémon: it has one `name` and one `level`, but a whole list of `moves`. To represent that, you store an array inside an object as one of its properties:
const zap = {
name: "Zap",
level: 4,
moves: ["Spark", "Thunder"]
};
console.log(zap.name);
console.log(zap.moves.length);
console.log(zap.moves[0]);Once again, read `zap.moves[0]` from left to right: `.moves` accesses the array property on `zap`, and `[0]` grabs the first move inside that array.
Exercise 2 — Every Array Method Applies
Because `zap.moves` is a real array, you can use every array property and method directly on it — like `.push()`, `.includes()`, or `.length`:
const zap = {
name: "Zap",
moves: ["Spark", "Thunder"]
};
zap.moves.push("Charge");
console.log(zap.moves.length);
console.log(zap.moves.includes("Charge"));You can also loop over it with `forEach` just like any standalone array:
const zap = {
name: "Zap",
moves: ["Spark", "Thunder"]
};
zap.moves.forEach((move) => {
console.log(zap.name + " knows " + move);
});Exercise 3 — Quiz: Into the Array
What does this code print?
const bag = {
owner: "Sam",
items: ["Potion", "Map", "Rope"]
};
console.log(bag.items[1]);MapPotion3undefinedHint : Reach the array first, then pick a position in it.
Exercise 4 — Count the Moves
Print how many moves Zap knows.
Hint : Reach the array through its property, then measure it as usual.
Exercise 5 — Several Arrays in One Object
Nothing stops an object from holding several lists. A trainer has a team, a bag and a list of badges:
const trainer = {
name: "Sam",
team: ["Aquachu", "Zap"],
bag: ["Potion", "Map"],
badges: ["Rock", "Water", "Fire"]
};
console.log(trainer.team.length + " Codémon");
console.log(trainer.bag.length + " items");
console.log(trainer.badges.length + " badges");Each property is independent. Adding a badge doesn't touch the team, and the object itself keeps the same three keys.
Exercise 6 — Methods That Use Their Own Arrays
An object holding a list often comes with methods to manage it. Inside them, `this` gives access to the array:
const zap = {
name: "Zap",
moves: ["Spark"],
learn(move) {
this.moves.push(move);
console.log(this.name + " learned " + move);
},
moveCount() {
return this.moves.length;
}
};
zap.learn("Thunder");
console.log(zap.moveCount());The object now manages its own data. Nobody outside needs to know that `moves` is an array.
Exercise 7 — Quiz: After the Method
What does this code print?
const bag = {
items: ["Potion"],
add(item) {
this.items.push(item);
}
};
bag.add("Map");
bag.add("Rope");
console.log(bag.items.length);321undefinedHint : Count what was there at the start, then what each call added.
Exercise 8 — Learn a Move
Complete the method so it adds the move to the object's own list.
Hint : Refer to the object without naming it, then add at the end of the list.
Exercise 9 — Reaching Inside
`const o = { list: [10, 20] };`. Match each expression with its value.
o.listo.list.lengtho.list[1]o.list[5]220undefinedExercise 10 — Trainer Profile
Complete the trainer object so it manages its own lists.
Step 1: add a method `catchCodemon(name)` that adds the name to the team and prints `"Sam caught name!"`. Step 2: add a method `report()` that prints `"Team: N"` and `"Badges: N"` with the two lengths, then one line per team member with `"- "` in front. Step 3: catch Metachu, then call the report.
Expected output:
Sam caught Metachu!
Team: 3
Badges: 2
- Aquachu
- Zap
- Metachuconst trainer = {
name: "Sam",
team: ["Aquachu", "Zap"],
badges: ["Rock", "Water"],
// Write your methods here
};
// Use them here