javascript - 打字稿中的地图初始化
问题描述
我正在尝试在打字稿中初始化下面的地图。当我打印它时,它似乎是空的。
let map: Map<string, object> = new Map<string, object> ([
[
"api/service/method",
{
uriPath: {}
}
],
[
"\"type\": \"html\"",
{
body: {}
}
]
]);
console.log(JSON.stringify(map));
// printing {}
// but not the initialized values
解决方案
它已正确初始化,但打印它不会像数组一样打印所有值。但是您可以通过访问密钥来检查它 - 它们存在:
let map = new Map([
[
"api/service/method",
{
uriPath: {}
}
],
[
"\"type\": \"html\"",
{
body: {}
}
]
]);
console.log(map);
console.log(map.get('api/service/method'));
console.log(map.get('"type": "html"'));
也相关:如何在控制台中显示 javascript ES6 地图对象?
如那里所述,您可以传播地图并打印:
let map = new Map([
[
"api/service/method",
{
uriPath: {}
}
],
[
"\"type\": \"html\"",
{
body: {}
}
]
]);
console.log([...map]);
推荐阅读
- r - 使用 sf 在 R 中查找时间和空间上最近的邻居
- python - Django激活翻译项目范围而不是每个视图?
- powerbi - 在 PowerBI 中,我们如何创建一个计算列,其中值被修改为唯一?
- vb.net - 如何在 WFP vb.net 的 Richtextbox 中添加大括号
- flutter - 为什么我的 Flutter Web 应用调试无法在浏览器中打开?
- noflo - 用于 UI 的网页中嵌入的 noflo 的简单示例
- bash - 无法在 shell 脚本中将参数传递给 curl 命令
- area - Halcon - 选择 Y 值最高的子区域
- java - 在 ElasticSearch 版本 7 中替换 InternalSimpleValue 构造函数
- matlab - 如何从具有高斯噪声的输出中找到变换矩阵?