[英]How can I use nested destructuring with objects with reused keys?
I have a message from my websocket: 我收到来自网络套接字的消息:
var message = {
spell: {
symbol: 'my-spell'
},
target: {
symbol: 'my-target'
}
};
I'm trying to learn destructuring, so I wrote the following code: 我正在尝试学习解构,因此我编写了以下代码:
let {
spell: {
spell_symbol: symbol
},
target: {
target_symbol: symbol = null
}
} = message;
console.log('spell symbol: ' + spell_symbol);
console.log('target symbol: ' + target_symbol);
This gives me an error: 这给我一个错误:
SyntaxError: Identifier 'symbol' has already been declared
Have I written the syntax wrong, or can you not reuse keys like symbol
in your nested object? 我的语法写错了吗,还是不能在嵌套对象中重用
symbol
键? How would I extract target.symbol
from message
? 如何从
message
提取target.symbol
?
You mixed up the order of property name and target expression, it should be 您混合了属性名称和目标表达式的顺序,应该是
const {
spell: {
symbol: spell_symbol
},
target: {
symbol: target_symbol = null
}
} = message;
console.log('spell symbol: ' + spell_symbol);
console.log('target symbol: ' + target_symbol);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.