Reputation: 2782
Any way I can do something like this? I need specific names for values...
const {
firstNm: 'my-funny-first-name',
lastNm: 'bar-foo_Bar'
} = response;
Upvotes: 0
Views: 196
Reputation: 48693
You need to change my-funny-first-name
to myFunnyFirstName
, because you cannot have dashes in a variable name.
var response = {
"firstNm" : "Billy-Bob",
"lastNm" : "Foo Bar"
};
const {
firstNm: myFunnyFirstName,
lastNm: barFooBar
} = response;
console.log(`myFunnyFirstName = ${myFunnyFirstName}\nbarFooBar = ${barFooBar}`);
.as-console-wrapper { top: 0; max-height: 100% !important; }
Upvotes: 0
Reputation: 18609
Variable names can't contain hyphens (-
), so you can't.
But, you can still rename the destructured properties, as long as their names are valid identifiers:
const {
firstNm: my_funny_first_name,
lastNm: bar_foo_Bar
} = response;
Note that you can't quote those names, but you don't have to at all:
Upvotes: 1
Reputation: 6037
You can do this, however you need to use valid variable names and you can omit the quotes. In other words:
const { firstNm: myFunnyFirstName, lastNm: barFooBar } = response
^ This will create new variables myFunnyFirstName
and barFooBar
to represent response.firstNm
and response.lastNm
, respectively.
Upvotes: 0