-2

A JSON data has 2 arrays (categories & applets) as shown below. Each applet can belong to many categories as per the data.

categories = ['Investments', 'Operations', 'Performance'];

applets = [
  {
    name: 'Performance Snapshot',
    categories: ['Performance'],
  },
  {
    name: 'Commitment Widget',
    categories: ['Investments'],
  },
  {
    name: 'CMS',
    categories: ['Investments', 'Performance'],
  },
];

I need to convert that JSON data to use them as category wise as shown below. How to do this in Typescript??

categories = [
    {
       'name': 'Performance',
       'applets': ['CMS', 'Performance Snapshot']
    },
    {
        'name' : 'Investments',
        'applets' : ['Commitment Widget', 'CMS']
    },
    {
        'name' : 'Operations',
        'applets' : []
    }
]
Mr.V
  • 17
  • 2

1 Answers1

0
    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;
   })
user2349470
  • 198
  • 8