使用带连字符的键解析对象

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

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

是否可以解构连字符键?我已经尝试将它变成一个计算属性,骆驼套管它,并使它成为一个普通的字符串但无济于事。有什么建议?

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

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

const { page, "per-page", total } = response.headers;
javascript ecmascript-6 destructuring
1个回答
3
投票
const { page, "per-page": perPage, total } = response.headers;

这将使用“每页”键将值写入perPage常量。

© www.soinside.com 2019 - 2024. All rights reserved.