javascript - 如何将 [{"tag":"google","count":1},{"tag":"microsoft","count":2}] 转换为:{"google":1, "microsoft" :2} 在打字稿中
问题描述
如何转换:
[
{
"tag":"google",
"count":1
},
{
"tag":"microsoft",
"count":2
}
]
类似于:
{
"google":1,
"microsoft":2
}
在打字稿中使用内置函数或任何快捷方式?
解决方案
var arr=[
{
"tag":"google",
"count":1
},
{
"tag":"microsoft",
"count":2
}
];
var k=arr.reduce((a, { tag, count }) => Object.assign(a, { [tag]: count }), {});
console.log(k);
一定的表现就是这个意思。
推荐阅读
- python - HTTPConnectionPool(host='backup.grammarbot.io', port=80): 超过 url 的最大重试次数:
- node.js - 一个 Collection 存储 2 个 email 地址,其中一个地址需要设置为默认
- mysql - AWS 无服务器 Aurora MySQL
- caching - 如何在缓存块的标记字段中查找位数
- c# - 如何防止 WPF 窗口在选择时调整大小?
- nginx - 遇到 nginx 反向代理的奇怪问题 - 需要帮助
- c# - 如何在 Asp.net Core 中一键从多个第三方 API 获取数据
- python-3.x - Google Colab 语法错误:语法无效
- c# - 如何在选择时修复 WPF 数据网格上移动的单元格?
- java - 从ChipNavigationBar选择主页按钮时如何打开activity_main.xml而不是片段?