The most straightforward approach, given the inability to destructure into objects, is:
const getTeamData =
({name: teamName, city: teamCity, state: teamState, mascot: teamMascot}) =>
({teamName, teamCity, teamState, teamMascot});
Various approaches have been suggested for destructuring into objects, which would allow the following kind of syntax:
function getTeamData(team) {
return team.{name: teamName, city: teamCity, state: teamState, mascot: teamMascot};
}
or
function getTeamData(team) {
return { {name: teamName, city: teamCity, state: teamState, mascot: teamMascot} = team };
}
But the powers that be on TC39 have shown no interest in such alternatives.
Proxies
Alternatively, since all you seem to want to be doing is to access the name
field as teamName
, consider using a proxy:
function getTeamData(team) {
return new Proxy(team, {
get(target, prop) { return target[`team${capitalize(prop)}`]; }
});
}