Home > Blockchain >  Replaced Nested Object Value with separate Object with a different structure
Replaced Nested Object Value with separate Object with a different structure

Time:03-27

I have an object of NBA Team names structured like so:

const HomeAndAwayTeams =
{
  teams: {
    '0': { hTeam: 'San Antonio', AwTeam: 'New Orleans' },
    '2': { hTeam: 'Sacramento', AwTeam: 'Orlando' 
},
    '4': { hTeam: 'Indiana', AwTeam: 'Toronto' }, 
    '6': { hTeam: 'Chicago', AwTeam: 'Cleveland' },
    '8': { hTeam: 'Brooklyn', AwTeam: 'Miami' },  
    '10': { hTeam: 'Milwaukee', AwTeam: 'Memphis' 
},
    '12': { hTeam: 'Oklahoma City', AwTeam: 'Denver' },
    '14': { hTeam: 'Houston', AwTeam: 'Portland' }  }
}

I'm looking to replace the City Names with their respective Team Nicknames.

I have an object setup that has their nicknames as the values, and the city names as the keys.

              const NBATeamsNickNames = {
                  "Atlanta": "Hawks",
                  "Boston": "Celtics",
                  "Brooklyn": "Nets",
                  "Charlotte": "Hornets",
                  "Chicago": "Bulls",
                  "Cleveland": "Cavaliers",
                  "Dallas": "Mavericks",
                  "Denver": "Nuggets",
                  "Detroit": "Pistons",
                  "Golden State": "Warriors",
                  "Houston": "Rockets",
                  "Indiana": "Pacers",
                  "LA": "Clippers",
                  "Los Angeles": "Lakers",
                  "Memphis": "Grizzlies",
                  "Miami": "Heat",
                  "Milwaukee": "Bucks",
                  "Minnesota": "Timberwolves",
                  "New Orleans": "Pelicans",
                  "New York": "Knicks",
                  "Oklahoma City": "Thunder",
                  "Orlando": "Magic",
                  "Philadelphia": "76ers",
                  "Phoenix": "Suns",
                  "Portland": "Trailblazers",
                  "Sacramento": "Kings",
                  "San Antonio": "Spurs",
                  "Toronto": "Raptors",
                  "Utah": "Jazz",
                  "Washington": "Wizards"                     
              }

I've tried several ways to replace the values, but I can't figure out how to access it if it was nested.

For example, I've tried something like this:

for (const key in NBATeamsNickNames) if (key in HomeAndAwayTeams) HomeAndAwayTeams = NBATeamsNickNames[key];

Which of course, only works if the object values in HomeAndAwayTeams weren't nested, and at base level.

I've attempted some other methods like a for loop, and a map, but I'm inexperienced in iterating over objects, especially in this format, and I'm trying to understand the concept behind accessing data in this way and replacing it in this case.

The number of teams in the HomeAndAwayTeams will change every day, it won't be a static amount. And of course, the teams within the object will change every day as well.

I appreciate any help, thank you.

CodePudding user response:

I guess you don't need to iterate over HomeAndAwayTeams object, you need to iterate over HomeAndAwayTeams.teams. You can use Object.keys method to do it.

This will change your object in place.

const { teams } = HomeAndAwayTeams
      
for (const key of Object.keys(teams)) {
  if (teams[key].hTeam in NBATeamsNickNames) {
    teams[key].hTeam = NBATeamsNickNames[teams[key].hTeam]
  }
  
  if (teams[key].AwTeam in NBATeamsNickNames) {
    teams[key].AwTeam = NBATeamsNickNames[teams[key].AwTeam]
  }
}
  • Related