包含带有特殊字符的字段的 JavaScript 解构对象

l00*_*00k 5 javascript destructuring

我有数组(API 响应):

let arr = [
    { '@type': 'Something', data: 1234 },
    { '@type': 'Something', data: 3214 },
]
Run Code Online (Sandbox Code Playgroud)

是否可以使用那些“@”前缀字段来解构元素?

for (const { data, ??? @type } of arr) {}
Run Code Online (Sandbox Code Playgroud)

Nin*_*olz 5

您可以采用计算属性和新变量名称。

let arr = [{ '@type': 'Something', data: 1234 }, { '@type': 'Something', data: 3214 }];

for (const { data, ['@type']: renamed } of arr) {
    console.log(renamed);
}
Run Code Online (Sandbox Code Playgroud)