将嵌套对象值替换为具有不同结构的单独对象
Replaced Nested Object Value with separate Object with a different structure
我有一个 NBA 球队名称的对象,结构如下:
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' } }
}
我希望用各自的团队昵称替换城市名称。
我有一个对象设置,将昵称作为值,将城市名称作为键。
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"
}
我已经尝试了多种方法来替换这些值,但是如果它是嵌套的,我不知道如何访问它。
例如,我试过这样的事情:
for (const key in NBATeamsNickNames) if (key in HomeAndAwayTeams) HomeAndAwayTeams = NBATeamsNickNames[key];
这当然只有在 HomeAndAwayTeams 中的对象值未嵌套且处于基本级别时才有效。
我尝试过一些其他方法,例如 for 循环和映射,但我在迭代对象方面缺乏经验,尤其是在这种格式下,我正在尝试理解 概念以这种方式访问数据并在这种情况下替换它。
HomeAndAwayTeams 的队伍数量每天都会变化,不会是一个固定的数量。当然,对象内的队伍也会每天更换。
感谢任何帮助,谢谢。
我猜你不需要迭代 HomeAndAwayTeams
对象,你需要迭代 HomeAndAwayTeams.teams
。您可以使用 Object.keys 方法来完成。
这将改变您的对象。
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]
}
}
我有一个 NBA 球队名称的对象,结构如下:
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' } }
}
我希望用各自的团队昵称替换城市名称。
我有一个对象设置,将昵称作为值,将城市名称作为键。
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"
}
我已经尝试了多种方法来替换这些值,但是如果它是嵌套的,我不知道如何访问它。
例如,我试过这样的事情:
for (const key in NBATeamsNickNames) if (key in HomeAndAwayTeams) HomeAndAwayTeams = NBATeamsNickNames[key];
这当然只有在 HomeAndAwayTeams 中的对象值未嵌套且处于基本级别时才有效。
我尝试过一些其他方法,例如 for 循环和映射,但我在迭代对象方面缺乏经验,尤其是在这种格式下,我正在尝试理解 概念以这种方式访问数据并在这种情况下替换它。
HomeAndAwayTeams 的队伍数量每天都会变化,不会是一个固定的数量。当然,对象内的队伍也会每天更换。
感谢任何帮助,谢谢。
我猜你不需要迭代 HomeAndAwayTeams
对象,你需要迭代 HomeAndAwayTeams.teams
。您可以使用 Object.keys 方法来完成。
这将改变您的对象。
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]
}
}