admin管理员组文章数量:1305173
I have an object:
let obj = {
"t":
{
"en": {
"head" :"hello",
"sub" : "this is great"
}
}
"link": {}
}
and I'm destructuring it:
{ t: { en }}
is it possible to reassign t
to en
in the same destructuring statement?
So I want the object assigned to en
in the variable t
not in en
I have an object:
let obj = {
"t":
{
"en": {
"head" :"hello",
"sub" : "this is great"
}
}
"link": {}
}
and I'm destructuring it:
{ t: { en }}
is it possible to reassign t
to en
in the same destructuring statement?
So I want the object assigned to en
in the variable t
not in en
3 Answers
Reset to default 8When you're destructuring an object, you can specify propname: variablename
to assign the property to a variable with a different name. Putting just the property name without a variable is equivalent to propname: propname
, to use the same name for the variable and property, but you don't need to use that shortcut.
So you can do it like this:
let obj = {
"t":
{
"en": {
"head" :"hello",
"sub" : "this is great"
}
}
}
let { t: { en: t } } = obj;
console.log(t);
The first t:
is just a property name, it specifies that the value is an object which needs to be destructured as well. In that structure, we use the propname: variablename
syntax, so the en
property is assigned to the t
variable.
In a function definition this would look like:
const Home = ({ t: { en: t }, link}) => console.log(t);
let obj = {
"t":
{
"en": {
"head" :"hello",
"sub" : "this is great"
}
}
};
Home(obj);
Note that there's no actual relationship between the two uses of t
. The variable can actually be anything, e.g.
const Home = ({ t: { en: blah }, link}) => console.log(blah);
No, it's impossible to reassign t
, because t
is a const
. If t
weren't a const
, then yes, it would be possible to reassign it:
let t = {
"en":
{
"head": "hello",
"sub" : "this is great"
}
};
({ en: t } = t);
console.log(t);
Note that
{ t: <something>
would only be done with destructuring if t
were a property of the parent object - but here, t
is a standalone variable, not a property so instead, start with
{ en: <something>
with t
on the right-hand side.
If t
actually is a property of the parent object, then you can do the same sort of thing:
let obj = {
"t":
{
"en": {
"head" :"hello",
"sub" : "this is great"
}
},
"link": {}
};
const { t: { en: t } } = obj;
console.log(t);
You could do this in one line in the following manner:
let obj = {
"t":
{
"en": {
"head" :"hello",
"sub" : "this is great"
}
}
}
// here we are destructuring en but declaring
// it as a variable which is called t
let { en: t } = obj.t;
console.log(t)
We can choose an alias in a destructuring assignment using a colon.
本文标签: javascriptDestructure object and reassign to different variable name in one goStack Overflow
版权声明:本文标题:javascript - Destructure object and reassign to different variable name in one go - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741799080a2398114.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论