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

问题描述 投票:1回答:1

我有数组(API响应):

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

是否可以用那些带有'@'前缀的字段来分解元素?

for (const { data, ??? @type } of arr) {}
javascript destructuring
1个回答
2
投票

您可以使用computed property和新的变量名。

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

for (const { data, ['@type']: renamed } of arr) {
    console.log(renamed);
}
© www.soinside.com 2019 - 2024. All rights reserved.