[英]How to convert the JSON data into Key Value Pair using Typescript
一個 JSON 數據有 2 個 arrays(類別和小程序),如下所示。 根據數據,每個小程序可以屬於許多類別。
categories = ['Investments', 'Operations', 'Performance'];
applets = [
{
name: 'Performance Snapshot',
categories: ['Performance'],
},
{
name: 'Commitment Widget',
categories: ['Investments'],
},
{
name: 'CMS',
categories: ['Investments', 'Performance'],
},
];
我需要將 JSON 數據轉換為按類別使用它們,如下所示。 如何在 Typescript 中做到這一點?
categories = [
{
'name': 'Performance',
'applets': ['CMS', 'Performance Snapshot']
},
{
'name' : 'Investments',
'applets' : ['Commitment Widget', 'CMS']
},
{
'name' : 'Operations',
'applets' : []
}
]
const result = categories.map((category) => {
const temp = {};
const filteredApplets = applets.filter((ele) =>
ele.categories.includes(category)).map(({name}) => name);
temp['name'] = category;
temp['applets'] = filteredApplets;
return temp;
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.