使用带连字符的键解构 objects

Destructuring objects with hyphenated keys

在我的应用程序中,我正在根据响应 header 向我的商店添加一些分页元数据。其中一个键的名称是 Per Page,它在 object 文字中转换为 per-page

完全可以解构带连字符的键吗?我试过将它变成一个计算 属性、camel-casing 它,并把它变成一个纯字符串但无济于事。有什么建议吗?

const { page, [per-page], total } = response.headers;

const { page, perPage, total } = response.headers;

const { page, "per-page", total } = response.headers;
const { page, "per-page": perPage, total } = response.headers;

这会将带有键 "per-page" 的值写入 perPage 常量。