I'm working on a project that requires me to print out all objects, I have to use nested for in loops.
The result should be printed like this:
team: Manchester United
stadium:
name:The Valley
capacity:65000
league: League 1
kit:
home: blue and white
away:light blue`
Here's what I came up with:
function footballClub() {
let club = {
team: "Manchester United",
stadium: {
name: "The Valley",
capacity: 65000
},
league: "League1",
kit: {
home: "blue and white",
away: "light blue"
}
}
for (let outerKey in club) {
for (let innerKey in club[outerKey]) {
if (typeof club[outerKey === club.stadium.hasOwnProperty('name')]) {
console.log(outerKey + ": " + innerKey + ": " + club[outerKey].name);
}
console.log(outerKey + ": " + club[outerKey]);
}
}
}
I'm stuck because it just repeats the team, stadium etc. 8 times because of the outer and inner loops.
I can't seem to print each inner objects as it always prints the name
, I've tried adding capacity
, home
, and away
but it never prints them, so I have to remove them from the code.
Is there a way to print all outer objects and inner objects dynamically and without repeating itself 8 times?