【问题标题】:How to convert the JSON data into Key Value Pair using Typescript如何使用 Typescript 将 JSON 数据转换为键值对
【发布时间】:2021-04-03 16:24:59
【问题描述】:

一个 JSON 数据有 2 个数组(类别和小程序),如下所示。每个小程序根据数据可以属于多个类别。

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' : []
    }
]

【问题讨论】:

    标签: arrays json typescript keyvaluepair


    【解决方案1】:
        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;
       })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-26
      • 1970-01-01
      • 2019-09-13
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      相关资源
      最近更新 更多