1

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?

Jazib
  • 1,343
  • 13
  • 27
reiko1997
  • 21
  • 6
  • This post might help https://stackoverflow.com/questions/8312459/iterate-through-object-properties – Alex Leo Feb 08 '20 at 17:20
  • Does this answer your question? [Get all keys of a nested dictionary](https://stackoverflow.com/questions/39233973/get-all-keys-of-a-nested-dictionary) – DCR Feb 08 '20 at 17:31

2 Answers2

0

Try this:

const
  club = {
    team: 'Manchester United',
    stadium: {
      name: 'The Valley',
      capacity: 65000
    },
    league: 'League1',
    kit: {
      home: 'blue and white',
      away: 'light blue'
    }
  },
  result = Object.entries(club).map(([key, value]) =>
    key + ': ' + (typeof value === 'string' ?
      value :
      Object.entries(value).map(([innerKey, innerValue]) =>
        `\n\t${innerKey}: ${innerValue}`).join(''))
  ).join('\n');
  
console.log(result)
Nave Sade
  • 441
  • 2
  • 6
0

Slight changes to code. Just check value is object or not before navigating in inner loop. Problem in current code is for in loop is going over string value.

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) {
    if (typeof club[outerKey] !== "string") {
      for (let innerKey in club[outerKey]) {
        console.log(
          outerKey + ": " + innerKey + ": " + club[outerKey][innerKey]
        );
      }
      console.log('');
    } else {
        console.log(outerKey + ": " + club[outerKey]);
        console.log('');
    }
  }
}

footballClub();
Siva K V
  • 10,561
  • 2
  • 16
  • 29