node.js - 我可以在 Json 的关键位置使用枚举吗
问题描述
我正在使用打字稿 Nodejs(MEAN 堆栈)
尝试enum
在 json 的属性/模式中使用
一个示例枚举就像
Enum KeyEnums {
A: "featureA",
B: "featureB",
C: "featureC"
}
预期的 JSON
{
featureA: "This is feature A",
featureB: "This is feature C",
featureC: "This is feature C"
}
我试图实现的方式
{
KeyEnums.A: "This is feature A",
KeyEnums.B: "This is feature C",
KeyEnums.C: "This is feature C"
}
但是,得到一个错误说
"Property or signature expected"
..无论如何要在 JSON 属性位置使用枚举?
解决方案
[
您应该像这样包装枚举键]
:
enum KeyEnums {
A = "featureA",
B = "featureB",
C = "featureC"
}
const a= {
[KeyEnums.A]: "This is feature A",
[KeyEnums.B]: "This is feature C",
[KeyEnums.C]: "This is feature C"
}
const json = JSON.stringify(a)
console.log(json)
推荐阅读
- c# - 在 Visual Studio 中调试 Web 应用程序时出现 Hangfire DistributedLockTimeoutException
- php - 如何在php中循环表格
- asp.net-core - 如何在 Asp.net 核心中获取文件路径以在 Azure Dev Ops 中工作
- python-3.x - 如何使用 tkinter 将滚动条连接到两个画布
- ffmpeg - 在 ffmpeg 中使用 if 语句
- php - 如何在 Wordpress 函数中简化此代码?
- javascript - 将最大值归因于jQuery中的变量
- javascript - 如何根据文字长度和样式计算拟合宽度
- shell - 用于创建多个 AWS IAM 用户的 shell 脚本
- elasticsearch - 如何在弹性搜索查找中正确使用通配符?