首页 > 解决方案 > 如何将 [{"tag":"google","count":1},{"tag":"microsoft","count":2}] 转换为:{"google":1, "microsoft" :2} 在打字稿中

问题描述

如何转换:

[
    {
        "tag":"google",
        "count":1
    },
    {
        "tag":"microsoft",
        "count":2
    }
]

类似于:

{
    "google":1,
    "microsoft":2
} 

打字稿中使用内置函数或任何快捷方式?

标签: javascripttypescript

解决方案


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);

一定的表现就是这个意思。


推荐阅读