这个问题在这里已经有了答案:
How to destructure object properties with key names that are invalid variable names?
(3 个回答)
上个月关门。
我有数组(API 响应):
let arr = [
{ '@type': 'Something', data: 1234 },
{ '@type': 'Something', data: 3214 },
]
是否可以使用带有“@”前缀的字段来解构元素?
for (const { data, ??? @type } of arr) {}
最佳答案
您可以使用 computed property和一个新的变量名。
let arr = [{ '@type': 'Something', data: 1234 }, { '@type': 'Something', data: 3214 }];
for (const { data, ['@type']: renamed } of arr) {
console.log(renamed);
}
关于包含带有特殊字符的字段的 JavaScript 解构对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59678817/