用不同的结构替换嵌套对象值
我有一个像这样构成的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];
当然,只有在没有嵌套的homeandawayteam中的对象值和基本级别时,才起作用。
我尝试了其他一些方法,例如for循环和地图,但是我在对象上没有经验,尤其是在这种格式上,我试图理解 概念 在这种情况下以这种方式访问数据并替换的背后。
HomeandawayTeams中的团队数量每天都会改变,这不会是静态的。当然,对象中的团队也会每天改变。
我感谢任何帮助,谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想您不需要迭代
homeandawayteams
对象,您需要迭代homeandawayteams.teams
。您可以使用 object.keys.keys 方法。这将更改您的对象。
I guess you don't need to iterate over
HomeAndAwayTeams
object, you need to iterate overHomeAndAwayTeams.teams
. You can use Object.keys method to do it.This will change your object in place.